2010-05-17 60 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对象。