![](/img/trans.png)
[英]Regex to remove all <br /> tags from beginning and end of a string in javascript
[英]Remove whitespace from beginning and end of string of all textbox present in a webpage using jQuery or javascript
是否有可能使用jQuery或javascript从网页(或jsp页面)中存在的所有文本字段中删除字符串开头和结尾的空格。 例如,假设我在一个页面中有50个文本字段,并且想要检查所有这些文本字段的空白(开头和结尾)。我认为单独为每个文本字段调用函数不是一个好主意。 如何在不为每个文本字段调用函数的情况下执行此操作。
请提出建议。
只能将val()
与回调参数一起使用。 它将为您遍历所有元素:
$('input[type=text]').val(function( index, originalValue){
return $.trim(originalValue);
});
使用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>
您可以执行以下代码:
$('input[type=text]').each(function (i, e) {
var $this = $(e);
$this.val($this.val().trim());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.