想象一下,我有一个包含大量文本字段的HTML表单
<input id="name" type="text" required>
<input id="surname" type="text" required>
<input id="address" type="text" required>
如何修剪所有文本字段上的前导和尾随空格&#34;焦点输出&#34;,即当用户离开填充文本字段时?
答案 0 :(得分:1)
一个简单的解决方案是使用jquery函数$.trim
//removes leading and trailing spaces on every text field "on focus out"
$( ":text" ).each(function( index ) {
$( this ).focusout(function() {
var text = $(this).val();
text = $.trim(text);
$(this).val(text);
});
});
====================
这是一个简单的代码
//removes leading and trailing spaces on every text field "on focus out"
$( ":text" ).each(function( index ) {
$( this ).focusout(function() {
var text = $(this).val();
text = $.trim(text);
$(this).val(text);
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
First Name<br>
<input id="name" type="text" required><br>
Surname<br>
<input id="surname" type="text" required><br>
Address<br>
<input id="address" type="text" required><br>
&#13;
答案 1 :(得分:0)
简单
$(function () {
$("input[type=text]").blur(function () {
$(this).val($(this).val().trim());
});
});