2013-04-11 27 views
0

嗨,這是送我圈。jquery檢測所選按鈕的ID?

考慮這些單選按鈕:

<input type="radio" class="win" id="50" value="1" name="match1" /> 
<input type="radio" class="cover" id="50" value="2" name="match1" /> 

<input type="radio" class="win" id="51" value="1" name="match2" /> 
<input type="radio" class="cover" id="51" value="2" name="match2" /> 

<input type="radio" class="win" id="52" value="1" name="match3" /> 
<input type="radio" class="cover" id="52" value="2" name="match3" /> 

當提交表單我想知道的任何一個選擇按鈕的ID。

可能根本沒有選擇某些按鈕對。

我想:

for (var x=1; x<4; ++x){ 

    if($('form #'+$(this).attr("id")+'input:radio[name=match'+x+']').prop('checked', true)){ 
      alert('Checked;); 
    } 

} 

但它總是evlautes爲true。

謝謝

+3

'id'屬性** **需求是唯一的! (*和他們不應該以一個數字開始,除非你使用html5 *) – 2013-04-11 14:51:33

+0

可能重複[如何檢查單選按鈕是否在javascript中被選中?](http://stackoverflow.com/questions/1423777/how-我可以檢查是否一個單選按鈕被選中在JavaScript) – Nix 2013-04-11 14:52:07

回答

1

您將值設置爲true。從prop呼叫刪除,true

if($('form #'+$(this).attr("id")+'input:radio[name=match'+x+']').prop('checked')){ 
     alert('Checked;); 
} 

當您設置屬性與.prop('checked', true)它返回你稱它在jQuery的元素 - 和計算結果爲true。

+0

我刪除了真實,現在它總是評估爲假! – 2013-04-11 15:06:08

+0

謝謝!我不得不刪除'表單ID',然後它工作...如此:if($('input:radio [name = match'+ x +']')。prop('checked')) – 2013-04-11 15:11:07

1

您是setting選中的財產,如果不是僅僅getting

變化

if($('form #'+$(this).attr("id")+'input:radio[name=match'+x+']').prop('checked', true)) 

if($('form #'+$(this).attr("id")+'input:radio[name=match'+x+']').prop('checked')) 
+0

你能告訴我在最新的jQuery版本中它仍然被棄用? – Adil 2013-04-11 14:53:43

+0

沒問題,謝謝分享鏈接和努力來幫助我,我會刪除我的。 – Adil 2013-04-11 14:59:17

0

我相信這將會回答你的問題:

for (var i = 0, len = 4; i < len; i++) { 
    if ($('input[name=match' + i + ']:checked').val()) { 
     alert("Checked"); 
    } 
} 
+0

謝謝你也工作 – 2013-04-11 15:35:53

0

這裏是爲您的方案的代碼的完整工作片,以實例的jsfiddle在:http://jsfiddle.net/CV4x8/

<input type="radio" class="win" id="50" value="1" name="match1" /> 
<input type="radio" class="cover" id="50" value="2" name="match1" /> 

<input type="radio" class="win" id="51" value="1" name="match2" /> 
<input type="radio" class="cover" id="51" value="2" name="match2" /> 

<input type="radio" class="win" id="52" value="1" name="match3" /> 
<input type="radio" class="cover" id="52" value="2" name="match3" /> 

<button id="getChecked"></button> 

function alertChecked() { 
    for (var i = 0, x = 4; i < x; i++) { 
     if ($('input[name=match' + i + ']').is(':checked')) { 
      alert("Checked"); 
     } 
    } 
} 

$('#getChecked').click(function() { 
    alertChecked(); 
}); 
+0

這也thnaks工作。 「之間有什麼區別。(':checked')「和」prop('checked')「 - 我應該使用另一個嗎 – 2013-04-11 15:11:56

+0

在效率方面不是100%確定的,但我覺得它更具可讀性,所以我可以回顧我的代碼並 – 2013-04-11 15:15:00

+0

這很奇怪,代碼工作,我可以檢測到哪些單選按鈕被檢查,然後我的腳本清除所有先前檢查過的選中按鈕,然後如果我重新選擇一些按鈕,並重新選擇 - 提交表單沒有被檢測到被選中! – 2013-04-11 15:43:12

0

首先不使用多個ID和原因,而是沒有不以數字開頭他們有效的HTML。

$("input[type='radio']:checked").each(function(index) { 
    $('#output').append($(this).attr('id')+' is checked<br />'); 
}); 

這裏是一個的jsfiddle你的情況的工作:http://jsfiddle.net/RGZH7/3/