[英]How can I get all the radio buttons in each group and check one is checked in each group?
[英]How can I check if all radio buttons are checked
我有多個帶有“是”或“否”選項的單選按鈕。
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
}
一種方法是檢查是否檢查了所有值為yes
的無線電
if($('input[type="radio"][value="yes"]').not(':checked').length == 0){
//all checked
}
您可以使用value != true
檢查單選按鈕的計數。 如果計數為零,則將選中所有單選按鈕。
if(!$('input[type="radio"][value="yes"]').not(':checked').length){
//select all radio buttons with value = yes
}
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <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> <input type="button" id="btn" value="Test" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#btn").click(function() { var length = 3; var isChecked = true; for (var i = 1; i <= length; i++) { isChecked = isChecked && ($("#yes" + i).is(":checked")); } if (isChecked) alert("All are checked"); else alert("All are not checked"); }); }); </script> </body> </html>
<input type="radio" id="yes1" name="group1" value="yes" checked>Yes<br>
<input type="radio" name="group1" value="No">No<br>
<hr>
<input type="radio" id="yes2" name="group2" value="yes" checked>Yes<br>
<input type="radio" name="group2" value="No">No<br>
<hr>
<input type="radio" id="yes3" name="group3" value="yes" checked>Yes<br>
<input type="radio" name="group3" value="No">No<br>
var yes1 = $("#yes1").is(":checked")
var yes2 = $("#yes2").is(":checked")
var yes3 = $("#yes3").is(":checked")
//$("#Myradio").is(":checked")
if (yes1 & yes2 & yes3) {
alert('sasa');
//do something
}
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <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> <input type="button" id="btn" value="Test" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("#btn").click(function() { var length = 3; var isChecked = true; for (var i = 1; i <= length; i++) { isChecked = isChecked && ($("#yes" + i).is(":checked")); } if (isChecked) alert("All are checked"); else alert("All are not checked"); }); }); </script> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.