开发者

How can I preselect a radio button using javascript or jquery?

开发者 https://www.devze.com 2023-02-01 21:43 出处:网络
I know this is probably easy, but I just can\'t figure it out. I am working with a SAAS shopping cart solution so I can\'t adjust the form code or use PHP, so I need to use javascript or jquery.开发者

I know this is probably easy, but I just can't figure it out.

I am working with a SAAS shopping cart solution so I can't adjust the form code or use PHP, so I need to use javascript or jquery.

开发者_高级运维

In this cart, I have a form....

<form method="post" action="cart.php" id="productDetailsAddToCartForm">  
Please Select a Bottle Size:  
<ul>  
<li><label><input name="variation[1]" type="radio" class="RadioButton" value="30"  /> 11oz</label></li> 
<li><label><input name="variation[1]" type="radio" class="RadioButton" value="31"  /> 33oz </label>  
</li>  
</ul>  
</form>

And I need to preselect the first radio button using jquery or javascript.

Can you help?


You can use the attr method with checked like this:

$('.RadioButton:first').attr('checked', true);

Or

$('.RadioButton:first').attr('checked', 'checked');

Note that :first will select the first radio button there.


$(':input:radio:eq(0)').attr('checked', 'checked');

This will check the first radio box on the page.

0

精彩评论

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

关注公众号