在jQuery中,我想選中所有沒有選中按鈕的單選按鈕組。jQuery和單選按鈕組
或者,有沒有一種方法可以選擇所有單選按鈕組並迭代組?
我正在動態地將N個單選按鈕組添加到一個頁面,並且在手之前不會知道單選按鈕組的名稱。
在jQuery中,我想選中所有沒有選中按鈕的單選按鈕組。jQuery和單選按鈕組
或者,有沒有一種方法可以選擇所有單選按鈕組並迭代組?
我正在動態地將N個單選按鈕組添加到一個頁面,並且在手之前不會知道單選按鈕組的名稱。
要查找所有的無線電組:
var radio_groups = {}
$(":radio").each(function(){
radio_groups[this.name] = true;
})
找到其無線電組已檢查單選按鈕,並且沒有:
for(group in radio_groups){
if_checked = !!$(":radio[name='"+group+"']:checked").length
alert(group+(if_checked?' has checked radios':' does not have checked radios'))
}
是什麼!代表爲什麼? – 2010-04-11 14:51:23
[Duble NOT,它連續轉換布爾值兩次,所以'!! true = true'](http://stackoverflow.com/questions/1406604/what-does-the-operator-double-exclamation-point-平均在JavaScript/1406618#1406618) – 2010-06-20 13:52:45
我知道這是舊的,但這已經幫助我這麼多!我只是想要一個簡單的方法來確保我的每個單選按鈕組被選中,並且在這裏。 – LittleTreeX 2011-03-07 01:59:50
支持多個團體和預檢查。
$('input').click(function() {
var $t = $(event.target);
if ($t.hasClass('checked')) $t.removeAttr('checked');
else $('input[type="radio"][name="' + $t.prop('name') + '"]').not($t).removeClass('checked');
$t.toggleClass('checked');
}).filter(':checked').addClass('checked');
```
要選擇按組名所有電臺和僅獲得不同名稱的列表:
function selectRadioByGroupName() {
return $.unique($('input:radio').map(function(index, element) {
return this.name;
}));
}
一個例子片斷:
function selectRadioByGroupName() {
return $.unique($('input:radio').map(function(index, element) {
return this.name;
}));
}
$(function() {
selectRadioByGroupName().each(function(index, element) {
$('body').append($('<p>First Group name is: ' + element + '</p>'));
});
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<form action="">
<p>
Q1:
</p>
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
<br />
<p>
Q2:
</p>
<input type="radio" name="status" value="male"> Single<br>
<input type="radio" name="status" value="female"> Married<br>
<input type="radio" name="status" value="other"> Other
<br />
<input type="button" name="submit_id" value="Submit" onclick="submitAnswers()">
</form>
得到不同組的名稱有可能週期對他們之後。
你可以把一個示例html? – 2009-07-22 15:02:13