开发者

Checking Number of Selected Checkboxes

I saw the is function in jQuery, and it returns me, for example, if any of the checkboxes are selected.

I need to do a check for how many checkboxes had been selected by the user, because my ap开发者_如何学运维p requires something like "select two of these". Is there a quick way to do it?

Also, how can I catch when the user clicks the "submit" and then be able to decide whether to let the form send or not, according to checks?


To get the amount of checkboxes checked and bind to the submit button:

$("#yourButton").click(function(e){
  var n = $("input:checked").length;  //Checkbox count, may need refined
  if(n<2) //using n from above.
  {
    e.preventDefault(); 
  }
});

Working Example: http://jsfiddle.net/ZtTG2/


$(".submitBtn").click(function(event) {
    if ($(":checked").length < 2) {
        event.preventDefault();
    } else {
        $('form').submit();
    }
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜