我正在尝试在动态文本框中搜索字符串“ sample”,但似乎无法使其正常工作。我的代码有什么问题?
var sentence = document.getElementsByName('sentence[]');
for (i=0; i<sentence.length; i++)
{
if( sentence[i].indexOf('sample') != -1 )
{
alert("String Found");
return false;
}
}
sentence[i]
是dom元素,您可能想使用sentence[i].value
像
var sentence = document.getElementsByName('sentence[]');
for (i = 0; i < sentence.length; i++) {
if (sentence[i].value.indexOf('sample') != -1) {
alert("String Found");
return false;
}
}
jQuery的
var valid = true;
$('input[name="sentence[]"]').each(function () {
if (this.value.indexOf('sample') != -1) {
valid = false;
//to stop the iteration
return false;
}
});
if (!valid) {
alert("String Found");
return false;
}
或者
var valid = $('input[name="sentence[]"]').filter(function () {
return this.value.indexOf('sample') != -1
}).length == 0;
if (!valid) {
alert("String Found");
return false;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句