如何使用JQuery检查单选按钮?

我在一组中有两个单选按钮,我想检查单选按钮是否已经使用JQuery检查,如何?

给定一组单选按钮:

  

您可以使用jQuery测试是否检查特定的一个,如下所示:

 if ($("#radio1").prop("checked")) { // do something } // OR if ($("#radio1").is(":checked")) { // do something } // OR if you don't have ids set you can go by group name and value // (basically you need a selector that lets you specify the particular input) if ($("input[name='radioGroup'][value='1']").prop("checked")) 

您可以按如下方式获取组中当前已检查的值:

 $("input[name='radioGroup']:checked").val() 
//the following code checks if your radio button having name like 'yourRadioName' //is checked or not $(document).ready(function() { if($("input:radio[name='yourRadioName']").is(":checked")) { //its checked } });
//the following code checks if your radio button having name like 'yourRadioName' //is checked or not $(document).ready(function() { if($("input:radio[name='yourRadioName']").is(":checked")) { //its checked } }); 

这是最佳做法

 $("input[name='radioGroup']:checked").val() 

也检查一下:

 $(document).ready(function() { if($("input:radio[name='yourRadioGroupName'][value='yourvalue']").is(":checked")) { //its checked } }); 

进一步采取一些答案 – 如果您执行以下操作,您可以检查是否已检查无线电组中的任何元素:

if ($('input[name="yourRadioNames"]:checked').val()){ (checked)或if (!$('input[name="yourRadioNames"]:checked').val()){ (未选中)