2010-05-17 23 views
1

HTML如何使用jQuery返回無線電檢查對象?

<input type="radio" name="rdName" id="uniqueID1" value="1" checked="checked"> 
<input type="radio" name="rdName" id="uniqueID2" value="2"> 
<input type="radio" name="rdName" id="uniqueID3" value="3"> 

jQuery的#1

$('input:radio[name=rdName]:checked').val(); 

jQuery的#2

$('input[name=rdName]:checked'); 

jQuery的#1變檢查收音機的價值,但我需要得到整個對象來獲取ID。 jQuery#2得到這個(從Chrome開發者控制檯)。對象「0」是我需要的實際對象,但我無法這樣做。

0: HTMLInputElement 
constructor: function Object() 
context: HTMLDocument 
length: 1 
prevObject: Object 
selector: input[name=rdName]:checked 
__proto__: Object 

任何想法如何隔離所需的對象?

回答

3

您可以訪問jQuery對象中的元素的數組:

var element = $('input[name=rdName]:checked')[0]; 

或者你可以使用get方法:

​​3210
+0

謝謝。甚至沒有想過將它作爲一個數組訪問。 – Kim 2010-05-17 12:21:18

0
$('input:radio[name=rdName]:checked').attr('id'); 
0

你的意思是這樣的:

alert($('input[name=rdName]:checked').get(0)); 
0
$(document).ready(function() { 
    $("input[name='rdName']").on({ 
    'change': function() { 
     $.each($("input[name='rdName']"), function() { 
     var ObjectId; 

     if ($(this).is(':checked')) { 
      console.log(this); /* Jquery "this" you want */ 
      ObjectId = $(this).attr('id'); /* Id of above Jquery object */ 
     } 
     }); 
    } 
    }); 
}); 

試試吧,我希望它能按照你的要求工作,因爲你只希望jQuery對象獲得選中的單選按鈕的屬性(id)。您可能會在FireBug中看到使用此代碼的jQuery對象。