开发者

How to return radio checked object with jQuery?

HTML

<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 gets the value of checked radio, but I need to get the whole object to get the ID. jQuery #2 get this (from Chrome Dev console). Object "0" is the actual object I 开发者_如何学Cneed, but I am unable to just that.

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

Any ideas how to isolate the needed object ?


You can access the elements in the jQuery object as an array:

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

Or you can use the get method:

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


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


You mean this:

  alert($('input[name=rdName]:checked').get(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 */
        }
      });
    }
  });
});

Give it a try, I hope it'll work according to your requirement as you want only jQuery Object to get attribute (id) of checked radio button. You may see jQuery object using this code in FireBug.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜