根据ID在jquery中设置单选按钮“选中” [英] Set radio button 'checked' in jquery based on ID

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

问题描述

我有两个单选按钮具有相同的名称,一个是默认选中。在从id选择时,如何在jQuery中选中或取消选中单选按钮?

I have two radio buttons with the same name, one is checked by default. How can you check or uncheck a radio button in jQuery when selecting from id?

我尝试过:

$('#radio1').attr('checked','checked');
$('#radio1').attr('checked', true);

没有什么似乎工作..任何想法?

Nothing seems to work.. any ideas?

谢谢!

推荐答案

您不能使用相同的ID(#radio1 )多次使用

You can not have same id (#radio1) more than once, use a class instead.

$('.radio1').attr('checked', true);
$('.radio2').attr('checked', true);

id

如果您要在上选中/取消选中,请点击,但您可以这样做:

If you want to check/uncheck on click however, you may do like:

$('#someid').click(function(){
  $('#radio1').attr('checked', true);
});

$('#someid').click(function(){
  $('#radio1').attr('checked', this.checked);
});

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

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