jquery对radio的操作:取值和赋值

 剑鱼论坛 发表于 2020年06月27日 JS/jQuery  132
剑鱼论坛
LV 8 研究生
最后在线:2年前
加入时间:5年前
主帖:143  跟帖:100

我们在使用jquery对HTML表单中的单选按钮radio进行取值和赋值该怎么实现?

我们先来看下面的代码。

假设有以下的HTML代码:

<input type="radio" name="test" value="1">
<input type="radio" name="test" value="2">

现在我们要通过jquery来对上面的单选按钮进行取值和赋值操作。继续往下看。

我们可以使用以下代码进行取值:

$("input[type='radio'][name='test']:checked").val();

当你在页面上选中某个项的时候,就可以通过上面的代码获取到被选中的项的值。

我们再来看如何赋值:

$("input[type='radio'][name='test'][value='1']").prop("checked",true);

上面这个代码就是实现radio赋值的,运行代码后,我们会发现值为1的那个项被选中了。

论坛主帖
288
论坛跟帖
316
在线访客
0
今日主帖
0
今日跟帖
0
今日注册
0
168
http://jianyuluntan.com/gentie.html
http://jianyuluntan.com/postzan.html
http://jianyuluntan.com/postcai.html
http://jianyuluntan.com/postshoucang.html
http://jianyuluntan.com/gentiezan.html
http://jianyuluntan.com/gentiecai.html
http://jianyuluntan.com/huifu.html
http://jianyuluntan.com/xiugai.html
http://jianyuluntan.com/shanchugentie.html
1
cmVsZWFzZV8w
0
© 2019 - 2024 剑鱼论坛 版权所有
Powered by JianYuLunTan
剑鱼论坛 976666861
http://jianyuluntan.com/feedback.html
http://jianyuluntan.com/qiandao.html
http://jianyuluntan.com/denglu.html
0
http://jianyuluntan.com/adenglu.html
0
剑鱼论坛