我在HTML表中有一个搜索,如果表中存在卡号,该按钮将按钮禁用按钮。这意味着,如果用户在文本框中输入" 123"及其在表中的存在,则禁用了提交按钮。我注意到,尽管每次匹配行都显示 window.alert ,但仅当卡号位于表的最后一行时,该按钮才被禁用。我在之后介入了参数(indexof(filter),但这会引起多个警报。
功能
<script>
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("cardNo");
filter = input.value.toUpperCase();
table = document.getElementById("table");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
if(input.value.length == 4){
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td)
{
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
window.alert("Card Already in Use");
$('#submit').prop('disabled', true);
} else {
tr[i].style.display = "none";
$('#submit').prop('disabled', false);
}
}
}
}
}
</script>
我正在使用像您这样的函数,而我正遇到同样的问题。因此,我试图解决创建计数器。因此,我不使用警报。尝试这样做:
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i;
input = document.getElementById("cardNo");
filter = input.value.toUpperCase();
table = document.getElementById("table");
tr = table.getElementsByTagName("tr");
// Loop through all table rows, and hide those who don't match the search query
if(input.value.length == 4){
//COUNTER
var count = 0;
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
//if (td) { // I don't think so you need to use this if. I didn't use on my function
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
//window.alert("Card Already in Use"); //commented to avoid to show it all the time
$('#submit').prop('disabled', true);
count = 1;
} else {
tr[i].style.display = "none";
if (count == 0) { // to make on the button
$('#submit').prop('disabled', false);
}
}
//} //commented
}
}
}
正如我所说,我在功能上插入了这些代码,并且效果很好。我没有尝试您的功能,我只是通过信息来调整您的功能。我希望我有帮助。