How to check multiple checkboxes checked using jquery?

前端 未结 5 1733
别那么骄傲
别那么骄傲 2021-01-19 02:27

i am new to the jquery, it is quite interesting, but i am having a little problem, i am populating multiple checkboxes from database using foreach loop like this,



        
相关标签:
5条回答
  • 2021-01-19 03:12

    If you want at least one checkbox checked, you can use this

    var somethingChecked = false;
    $("input[type=checkbox]").each(function() {
      if(this).is(':checked')) {
        somethingChecked = true;
      }
    });
    if(!somethingChecked) {
      alert("You haven't checked anything yet");
    }
    

    What this does is initialize a variable to false. Then the script loops through all inputs of type checkbox. If the item is checked, set the variable to true. Finally, check if the variable is still false. If it is, then show an error.

    0 讨论(0)
  • 2021-01-19 03:12

    Assuming you have #my_form as the ID of your form, you could do

    $("#my_form input[type=checkbox]:checked"). // ... do something
    

    to select and do something with the checked checkboxes. You can also do

    $("#my_form input[type=checkbox]").each(function(idx, elem) {
       var is_checked = $(this).prop("checked");
       // Do something with is_checked
    });
    

    to iterate through all the checkboxes and check whether they are checked or not.

    0 讨论(0)
  • 2021-01-19 03:24

    To find how many checkboxes are checked, you can use something like:

    var checkedNum = $('input[name="city[]"]:checked').length;
    if (!checkedNum) {
        // User didn't check any checkboxes
    }
    

    Since you're providing the same name attribute to all the checkboxes (from your PHP loop), you can use the selector input[name="city[]"] to target and find them all. But to find out how many specifically are checked, you can add the :checked selector. An alternative to this is using $('input[name="city[]"]').filter(":checked").

    Finally, !checkedNum will only pass if checkedNum is 0, since 0 is falsey. Any other number is truthy, and wouldn't satisfy the condition !checkedNum.


    References:

    • jQuery attribute equals selector: http://api.jquery.com/attribute-equals-selector/
    • :checked selector: http://api.jquery.com/checked-selector/
    • jQuery .length property: http://api.jquery.com/length/
    0 讨论(0)
  • 2021-01-19 03:26

    First of all id of the checkboxes should be unique.

    Do like this

    <? 
    $checkBoxCount = 0;
    foreach($cities as $city) { ?>
        <input type="checkbox" name="city[]" value="<?=$city->id?>" id="chkbox-<?=$checkBoxCount?>" />
    <? } ?>
    

    Now in jQuery check like this to get all the checkboxes thats checked.

    var checkCount = $("input[name='city[]']:checked").length;
    
    if(checkCount == 0)
    {
       alert("Atleast one checkbox should be checked.");
    }
    
    0 讨论(0)
  • 2021-01-19 03:28

    This code work well for me,here i convert array to string with ~

        <input type="checkbox" value="created" name="today_check"><strong>Created</strong>
        <input type="checkbox" value="modified" name="today_check><strong>Modified</strong>
     <a class="get_tody_btn">Submit</a>
    
    <script type="text/javascript">
        $('.get_tody_btn').click(function(){
            var vals = "";
            $.each($("input[name='today_check']:checked"), function(){  
                vals += "~"+$(this).val();  
            });
            if (vals){
                vals = vals.substring(1);
            }else{
                alert('Please choose atleast one value.');
            }
    
    
        });
    </script>
    
    0 讨论(0)
提交回复
热议问题