jQuery设置单选按钮 [英] jQuery set radio button

查看:131
本文介绍了jQuery设置单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个单选按钮.我想使用值或ID进行设置.

I am trying to set a radio button. I want set it by using the value or the id.

这是我尝试过的.

$('input:radio[name=cols]'+" #"+newcol).attr('checked',true);

newcol是单选按钮的ID.

也许需要进行一些编辑.

Maybe a little edit is in order.

有两组单选框,一组带有列,另一组带有行.所以我明白了不使用id的意义.我的错.我举个例子:

There are two sets of radio boxes one with cols and the other with rows. So I see the point in not using id's. My bad. So I have as an example:

<input type="radio" name="rows" class="listOfCols" 
   style="width: 50%; " value="Site"></input>

<input type="radio" name="cols" class="listOfCols" 
   style="width: 50%; "  value="Site"></input>

已删除ID,我需要设置正确的ID.

with the id's removed, and I need to set the correct one.

推荐答案

您的选择器将查找ID为newcol(以及该变量的值)的input:radio[name=cols]元素的后代.

Your selector looks for the descendant of a input:radio[name=cols] element that has the id of newcol (well the value of that variable).

请改用此方法(因为您还是要通过ID进行选择):

Try this instead (since you're selecting by ID anyway):

$('#' + newcol).prop('checked',true);

这里是一个演示: http://jsfiddle.net/jasper/n8CdM/1/

此外,从jQuery 1.6开始,更改属性的首选方法为.prop(): http://api. jquery.com/prop

Also, as of jQuery 1.6 the perferred method of altering a property is .prop(): http://api.jquery.com/prop

这篇关于jQuery设置单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆