开发者

Run jQuery function when a certain number of checkboxes are selected

开发者 https://www.devze.com 2023-03-06 16:16 出处:网络
I am trying to achieve the following with jQuery: Select max 3 check boxes out of 20, w开发者_JAVA技巧hen three check boxes are selected run jQuery function.If I understand correctly you want to run

I am trying to achieve the following with jQuery:

Select max 3 check boxes out of 20, w开发者_JAVA技巧hen three check boxes are selected run jQuery function.


If I understand correctly you want to run a function if three boxes are checked? To do that use

$(":checkbox").click(function(){

if($(":checkbox:checked").length==3))
  //run function here
});


I'm considering the example on the official documentation of Jquery: http://api.jquery.com/checked-selector/

<script>
    function myFunction(){

    }

    function countChecked() {
      var n = $("input:checked").length;
      if(n==3){
         myFunction();
      }
    }

    $(":checkbox").click(countChecked);

</script>
0

精彩评论

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