开发者

JavaScript version of .each loop using element type and id wildcard

Is there a JavaScript version of the following. It works as I want, but it looks a bit messy and I've read that sometimes breaking from an outer for loop within a .each loop is more neatly achieved with Javascript.

for(array to iterate over){
    var abSelected = false;
    $("input[type='checkbox'][id*=AB]:checked").each(function() {
      abSelected = true;
    }
    开发者_如何学Pythonif(abSelected){
      break;
    }
...do other stuff...
}

I think the if statement is unnecessary. I want to loop all checkboxes beginning with 'AB' and break from the outer for loop if any are selected.

Thanks.


you can simply check on the length of the selected checkboxes beginning with 'AB' collection returned by jQuery, if > 0, break:

for(array to iterate over){
    if($("input[type='checkbox'][id*=AB]:checked").length > 0)
        break;
...do other stuff...
}


you don't need to loop all the checkboxes.
You could just use $('input[type='checkbox'][id*=AB]:checked').length

This will get you the count of all checked checkboxes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜