Home  >  Article  >  Web Front-end  >  How to remove spaces from input value in jquery

How to remove spaces from input value in jquery

青灯夜游
青灯夜游Original
2021-11-25 12:02:583120browse

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)".

How to remove spaces from input value in jquery

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>

How to remove spaces from input value in jquery

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn