Home > Article > Web Front-end > How to remove spaces from input value in jquery
Jquery method to remove spaces from input values: 1. Use the val() method to obtain the input value, the syntax is "$("input").val()"; 2. Use the trim() method to remove spaces, the syntax "$.trim(input value)".
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.
How jquery removes spaces from input values
Implementation:
You need to get the input first value, this requires the use of the val() method: $("input").val()
After obtaining the value, you can use trim() Method to remove spaces from values
Implementation code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ var value=$("input").val(); $("input").val($.trim(value)); }); }); </script> <style type="text/css"> .intro { font-size: 120%; color: red; } </style> </head> <body> <input type="text" value=" hello "/><br><br> <button>去除input值的空格</button> </body> </html>
Related video tutorial recommendations:jQuery tutorial (video)
The above is the detailed content of How to remove spaces from input value in jquery. For more information, please follow other related articles on the PHP Chinese website!