Loop through checkboxes and count each one checked or unchecked

前端 未结 5 1324
野性不改
野性不改 2020-11-30 22:40

I\'ve run into a bit of an issue. Here\'s a brief explanation.

I have 12 check boxes on a standard form. What I need to do is loop through each of them and learn whi

5条回答
  •  执笔经年
    2020-11-30 23:19

    You can loop through all of the checkboxes by writing $(':checkbox').each(...).

    If I understand your question correctly, you're looking for the following code:

    var str = "";
    
    $(':checkbox').each(function() {
        str += this.checked ? "1," : "0,";
    });
    
    str = str.substr(0, str.length - 1);    //Remove the trailing comma
    

    This code will loop through all of the checkboxes and add either 1, or 0, to a string.

提交回复
热议问题