2012-01-04 128 views
18

我在一個組中有兩個單選按鈕,我想檢查單選按鈕是否選中使用JQuery,怎麼樣?如何檢查單選按鈕是否使用JQuery檢查?

+2

[如何獲得的單選按鈕的值其中H的可能重複被檢查,jquery](http://stackoverflow.com/questions/1083644/how-to-get-the-values-of-the-radio-button-which-has-been-checked-jquery) – matino 2012-01-04 10:22:03

+0

可能重複[特定單選按鈕的檢查被選中](http://stackoverflow.com/questions/2195125/check-of-specific-radio-button-is-checked) – 2012-07-20 14:31:53

回答

42

給定一組單選按鈕:

<input type="radio" id="radio1" name="radioGroup" value="1"> 
<input type="radio" id="radio2" name="radioGroup" value="2"> 

您可以測試是否如下特定的人使用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() 
+0

非常感謝。這非常有用 – user1557020 2014-08-10 07:22:21

7
 

//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 
    } 
}); 
 
+1

描述你的代碼實際上會有幫助(對於提問者 - 不是我) – BeRecursive 2012-01-04 12:25:17

0

檢查這一個,太:

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

這是最好的做法

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

採取了一些答案一步 - 如果你下面,你可以檢查無線電組中的任何元素已經檢查:

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

相關問題