2012-06-02 147 views
1
<input type="radio" name="a" /> 
<input type="radio" name="a" /> 
<input type="radio" name="b" /> 
<input type="radio" name="b" /> 

我想確保的radiobuttonboth group必須檢查,爲了這個,我嘗試:檢查單選按鈕是否被選中或不jQuery的

if($("input[type=radio,name=a]:checked").length > 0 && 
    $("input[type=radio,name=b]:checked").length > 0) 
{  
/* do something */ 
} 

但不工作,有誰能夠告訴我爲什麼?

回答

6
if($("input[type=radio][name=a]:checked").length > 0 && 
    $("input[type=radio][name=b]:checked").length > 0) 
{  
/* do something */ 
} 
+0

感謝您的完美回答 – gaurav

1

工作演示另一種方法:http://jsfiddle.net/Mwkjs/

代碼

$("input[type='radio']").click(function() { 

    if ($("input[name='a']").is(":checked") && $("input[name='b']").is(":checked")) { 
     /* do something */ 
     alert('do something'); 
    } 
});​ 
0

要選擇在幾個你應該將它們分開:[type = radio] [name = a] 對於輸入元素,如果你願意,可以省略type屬性比較,甚至可以省略單詞'input',因爲每個類型都有特定的選擇器:收音機,:複選框:提交,等...),所以在這種情況下,你可以同樣適用:

if($('[name= a]:radio:checked').length>0 && $('[name= b]:radio:checked').length>0) 
{ 
/*...*/ 
} 
1

做到這一點的最佳方式,如果你正在使用JQuery 1.9+

if ($("#selector").prop('checked')){ 
    //Code goes here 
} 

我知道這是一個老問題,如果有人仍然在尋找答案,回答:)

相關問題