[英]Remove whitespace from beginning and end of string of all textbox present in a webpage using jQuery or javascript
Is it possible to remove whitespace from beginning and end of an string from all the textfields present in a webpage(or jsp page) using jQuery or javascript. 是否有可能使用jQuery或javascript从网页(或jsp页面)中存在的所有文本字段中删除字符串开头和结尾的空格。 For example .Suppose I have 50 textfields in a page and I want to check all these textfields for whitespace(beginning and end).I don't find it to be a good idea to call the function for every textfield individually. 例如,假设我在一个页面中有50个文本字段,并且想要检查所有这些文本字段的空白(开头和结尾)。我认为单独为每个文本字段调用函数不是一个好主意。 How can I do it without calling a function for each textfield. 如何在不为每个文本字段调用函数的情况下执行此操作。
Please suggest. 请提出建议。
Can just use val()
with a callback argument. 只能将val()
与回调参数一起使用。 It will loop over all elements for you: 它将为您遍历所有元素:
$('input[type=text]').val(function( index, originalValue){
return $.trim(originalValue);
});
Get all the inputs from the page using jquery then run a loop, and for each element trim the value 使用jquery从页面获取所有输入,然后运行循环,并为每个元素修剪值
<body>
<input type="text" value=" abc " >
<input type="text" value=" def " >
<input type="button" id="remove" value="Remove">
<script type="text/javascript">
$(document).ready(function(){
$('#remove').click(function(){
var inputs = $('input[type=text]');
$.each(inputs, function(index,input){
$(input).val($(input).val().trim())
});
});
});
</script>
</body>
You can execute this code: 您可以执行以下代码:
$('input[type=text]').each(function (i, e) {
var $this = $(e);
$this.val($this.val().trim());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.