如何通过id检查5个输入框,如果它们存在且未禁用,则在jquery中循环。
我想包括输入框是否存在代码,以及所有输入框的以下代码:
$("#cf_2693809:not(:disabled), #cf_2693816:not(:disabled), #cf_2693823:not(:disabled), #cf_2693830:not(:disabled),#cf_2693837:not(:disabled)").each(function(){
....
}
考虑以下内容。
var myIds = [
"cf_2693809",
"cf_2693816",
"cf_2693823",
"cf_2693830",
"cf_2693837"
];
$("input:not(:disabled)").each(function(index, element) {
if (myIds.indexOf($(element).attr("id")) >= 0) {
// Do the thing
}
});
这会迭代每个不是disabled
的input
元素,如果它的ID在Array中,它就会执行一些操作。
您也可以尝试一个更好的选择器。
$("#cf_2693809, #cf_2693816, #cf_2693823, #cf_2693830, #cf_2693837").not(":disabled").each(function(){ ... });
示例
$(function() {
$("#cf_2693809, #cf_2693816, #cf_2693823, #cf_2693830, #cf_2693837").not(":disabled").each(function() {
console.log("Found " + $(this).attr("id"));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<input type="text" id="cf_2693808" />
<input type="text" id="cf_2693809" disabled />
<input type="text" id="cf_2693816" />
<input type="text" id="cf_2693823" />
<input type="text" id="cf_2693830" />
<input type="text" id="cf_2693837" />
<input type="text" id="cf_2693840" />
</div>