Checking against any item in array being checked?
I need some help shortening this code and am just a little off. Here is original code:
$("#MKDPT").change(function() {
if ($("#MKDPT").is(":checked") && $("#AMX1N").is(":not(:checked)") ||
("#ERXXN").is(":not(:checked)")) {
alert(this.id);
}
});
I will be adding a lot more id's. Can I just create an array of the ID's im making sure aren't checked and compare against it? Im not sure if you can check to see if any item in an array is checked, or do I need to do it by .length?
Example
$("#MKDPT").change(function() {
if (开发者_如何学编程$("#MKDPT").is(":checked") && $("#AMX1N").is(":not(:checked)") ||
("#ERXXN").is(":not(:checked)") || ("#ID").is(":not(:checked)") || ("#ID").is(":not(:checked)") ||
("#ID").is(":not(:checked)") || ("#ID").is(":not(:checked)") || ("#ID").is(":not(:checked)")) {
alert('foo');
}
});
this is what I want it to do, but am having trouble with:
$("#MKDPT").change(function() {
var ckd = new Array = ("AMX1N","ERXXN","ID","ID","ID","ID","ID","ID","ID")
if ($("#MKDPT").is(":checked") && ckd.is(":not(:checked)")) {
alert('foo');
}
});
Kane Leins
One way is to go about like this:
$("#MKDPT").change(function() {
var ckd = new Array ("AMX1N","ERXXN","ID");
for(var i = 0; i < ckd.length; i++)
{
if ($("#" + ckd[i]).is(":checked")) {
alert('foo');
break;
}
}
});
The for
loop goes through each of the array element and checks to see if any element with such id is checked and if yes, it alerts about that.
精彩评论