我有多个带有“是”或“否”选项的单选按钮。
jQuery是否有一种简单的方法来检查是否都选择了“是”?
HTML是:
<input type="radio" id="yes1" name="group1" value="yes">Yes<br>
<input type="radio" name="group1" value="No">No<br>
<hr>
<input type="radio" id="yes2" name="group2" value="yes">Yes<br>
<input type="radio" name="group2" value="No">No<br>
<hr>
<input type="radio" id="yes3" name="group3" value="yes">Yes<br>
<input type="radio" name="group3" value="No">No<br>
我猜这有点像
yes1 = $("#yes1").prop("checked", true);
yes2 = $("#yes2").prop("checked", true);
yes3 = $("#yes2").prop("checked", true);
if (yes1 & yes2 & yes3) {
// do something ?
}
最佳答案
您可以将具有['value=yes]
的元素的长度与具有['value=yes]
和属性:checked
的元素的长度进行比较:
if($('[value=yes]').length==$('[value=yes]:checked').length){
//all yes elements are checked
}