How to get all checked checkboxes

前端 未结 4 663
离开以前
离开以前 2020-11-27 03:30

I have a set of input checkboxes with the same name and I would like to determine which checkboxes have been checked using javascript, how can I achieve that? I know only ho

4条回答
  •  广开言路
    2020-11-27 03:47

    A simple for loop which tests the checked property and appends the checked ones to a separate array. From there, you can process the array of checkboxesChecked further if needed.

    // Pass the checkbox name to the function
    function getCheckedBoxes(chkboxName) {
      var checkboxes = document.getElementsByName(chkboxName);
      var checkboxesChecked = [];
      // loop over them all
      for (var i=0; i 0 ? checkboxesChecked : null;
    }
    
    // Call as
    var checkedBoxes = getCheckedBoxes("mycheckboxes");
    

提交回复
热议问题