查找所有选中的复选框和单选按钮

Oto*_*oma 2 javascript dom

我想找到所有已选中的复选框和单选按钮。虽然我可以这样做:

var abc = document.querySelectorAll(".some_class input[type='checkbox']:checked");
Run Code Online (Sandbox Code Playgroud)

我怎样才能真正同时找到复选框和单选按钮?纯 JavaScript。

Raj*_*esh 5

正如之前评论的那样,

“.some_class 输入:已选中”

样本:

function notify(){
  var els = document.querySelectorAll('input:checked');
  for(var i = 0; i< els.length; i++){
    console.log(els[i].type, els[i].value)
  }
}
Run Code Online (Sandbox Code Playgroud)
<input type="checkbox" value="1">1
<input type="checkbox" value="2">2
<input type="checkbox" value="3">3
<input type="checkbox" value="4">4
<input type="checkbox" value="5">5

<br/>

<input type="radio" name="test" value="1">1
<input type="radio" name="test" value="2">2
<input type="radio" name="test"value="3">3
<input type="radio" name="test" value="4">4
<input type="radio" name="test" value="5">5

<button onclick="notify()"> Check </button>
Run Code Online (Sandbox Code Playgroud)

但如果您有不同的复选框和单选选择器,您可以尝试以下操作:

function notify(){
  var els = document.querySelectorAll('.chks input[type="checkbox"]:checked, .rbs input[type="radio"]:checked');
  for(var i = 0; i< els.length; i++){
    console.log(els[i].type, els[i].value)
  }
}
Run Code Online (Sandbox Code Playgroud)
<div class="chks">
<input type="checkbox" value="1">1
<input type="checkbox" value="2">2
<input type="checkbox" value="3">3
<input type="checkbox" value="4">4
<input type="checkbox" value="5">5
</div>

<div class="rbs">
<input type="radio" name="test" value="1">1
<input type="radio" name="test" value="2">2
<input type="radio" name="test"value="3">3
<input type="radio" name="test" value="4">4
<input type="radio" name="test" value="5">5
</div>
<button onclick="notify()"> Check </button>
Run Code Online (Sandbox Code Playgroud)