开发者

select a radio button with jquery

<input type="radio" value="True" propertyname="AmortizationTermInYears" onchange="showAmortizationTermInYears();UpdateField(this);" name="AmortizationTermInYears" id="AmortizationTermInYears" amortizationterminyearsradio="true">

<input type="radio" value="False" propertyname="AmortizationTermInYears" onchange="showAmortizationTerm开发者_开发百科InYears();UpdateField(this);" name="AmortizationTermInYears" id="AmortizationTermInYears" checked="checked">

How can I select one of these radio buttons using Jquery when the page loads?

Thanks!


$('input[name=AmortizationTermInYears]:nth(0)').prop("checked","checked");


This will do the trick:

$(document).ready(function() {
   $("input[name='AmortizationTermInYears'][value='True']").attr("checked", "checked");
});

You need to search by both name and value then set the checked attribute.

Live test case.


Although the question is mostly about how to select an element, I do notice that everyone is using the attr() function and, usually, this is wrong because calling this function only works once. If you call this, then the user checks a different one, if you call this the second time, it will not work. the correct way to do it is to use prop()

$('input[name=AmortizationTermInYears]:nth(0)').prop('checked', true);


"id" attribute should not be used again. You have given id="AmortizationTermInYears" to both radio buttons. It is wrong according to standards. Set different ids to both radio buttons.

Also you can select radio button by specifying id:

$('#AmortizationTermInYears').attr( "checked", "checked" );


$('input[name=AmortizationTermInYears]');

or

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜