2011-08-12 54 views
8

我正在使用jQuery,並且我有一組單選按鈕都具有相同的名稱,但具有不同的值屬性。使用jQuery取消選擇一組單選按鈕

例如:

<input type = "radio" name = "thename" value="1"></input> 
<input type = "radio" name = "thename" value="2"></input> 
<input type = "radio" name = "thename" value="3"></input> 

我想使它所以他們都未被選擇。我的頁面的當前狀態有一個點擊。我該怎麼做呢?

回答

1

嘗試以下代碼:

$(input[name=thename]).removeAttr('checked'); 
3

嘗試使用此:(檢查,選定,ECT)$('input[type="radio"]').prop('checked', false);

使用jQuery的prop方法可以改變元件的特性。

+0

這個工作時,我添加了一個單引號後面的括號--- $('input [type =「radio」]')。 –

+0

@JimEvans謝謝。我不確定爲什麼直到現在我還沒有看到。我已經編輯了答案,有結束報價。 – Kyle

7

從jQuery 1.6開始,建議使用$("radio").prop("checked", false);

+1

對不起,這不適合我。我嘗試過這個。另外,如果我在頁面上有其他無線電元素,並且我不想將這種行爲應用於他們呢? – Carl

+1

只是嘗試改變選擇器。像'$(「input [name ='thename']」)' –

8
$("input:radio[name='thename']").each(function(i) { 
     this.checked = false; 
}); 

不知道爲什麼jQuery的道具不工作,這樣做...

+0

[jQuery's'prop()'here]的良好討論(http://stackoverflow.com/a/5876747/557612) – steveax

2

發表@matzahboy答案完美。

試過其他的方法,但這個工作最好的:

$(input[name=thename]).removeAttr('checked'); 
0
function resetRadio(name) { 
    $('#form input:radio[name=' + name + ']:checked').each(function() { 
     var $this = $(this); 
     $this.prop("checked", false); 
    }); 
} 

$('#form input:radio').on('dblclick', function() { 
    var $this = $(this); 
    var name = $this.prop('name'); 
    resetRadio(name); 
}); 

這使您可以雙擊收音機重置它們。

0

要取消一個名爲「namegroup」組的所有收音機,試試這個:

$("input[type=radio][name=namegroup]").prop("checked", false); 
0

這是簡單,通用的答案(我相信): $("input[name=NAME_OF_YOUR_RADIO_GROUP]").prop("checked",false);

對於這個特定的問題,我會用:

$("input[name=thename]").prop("checked",false);

希望這有助於

0

它爲我工作;

$('input[name="radioName"]').attr('checked', false); 
相關問題