设置单选按钮“选中” jquery中根据ID

发布于 2024-09-07 11:11:28 字数 220 浏览 1 评论 0原文

我有两个同名的单选按钮,默认情况下选中一个。从 id 选择时如何选中或取消选中 jQuery 中的单选按钮?

我尝试过:

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

似乎没有任何效果......有什么想法吗?

谢谢你!

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?

I've tried:

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

Nothing seems to work.. any ideas?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄凉少年不暖心 2024-09-14 11:11:28

您不能多次使用相同的 id (#radio1),请使用 class 代替。

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

id 应在每个页面的每个元素中使用一次。

但是,如果您想选中/取消选中点击,您可能会喜欢:

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

或者

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

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

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

The id should be used once per element per page.

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

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

Or

$('#someid').click(function(){
  $('#radio1').attr('checked', this.checked);
});
手长情犹 2024-09-14 11:11:28

如果您有这样的收音机:

<input type="radio" id="radio1" name="same-radio-name" />
<input type="radio" id="radio2" name="same-radio-name" />

那么,要选中第二个并取消选中第一个:

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

反之亦然:

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

另一种想法是,您是否将收音机用作 JQuery UI 按钮?如果是这样,您可能还需要刷新它,例如:

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

If you have the radios like:

<input type="radio" id="radio1" name="same-radio-name" />
<input type="radio" id="radio2" name="same-radio-name" />

So, to check the second and uncheck the first one:

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

Viceversa:

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

Just another thought, are you using the radio as a JQuery UI button? If so you may also need to refresh it like:

$('#radio1').attr('checked', true).button("refresh");
嘿咻 2024-09-14 11:11:28

好吧,如果您想从输入无线电中获取一个值以从数据库上传该值,您可以尝试使用以下简单的代码:

 var selValue = $('input[name=rbnNumber]:checked').val();

在其中放置变量“selValue”的名称,并在其中指定无线电组的名称“rbn 编号”。所有这些都在您工作和完成的功能内。

Well, if you wanna get a value from the input radio to upload this value from your database you can try with this simple code:

 var selValue = $('input[name=rbnNumber]:checked').val();

Where you put the name of the variable "selValue" and inside of this you specific the name of the radio group "rbnNumber". All this inside the function that you work and done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文