javascript 检查 JQuery 中的单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3914669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:40:53  来源:igfitidea点击:

Checking a radio button in JQuery

javascriptjqueryradio-button

提问by bba

I need to problematically(do you mean programmatically?)check a radio button given its value. The form has an idand the input type obviously has a name (but no id). The only code I managed to get working so far is:

我需要有问题地(您是说以编程方式吗?)检查给定值的单选按钮。表单有一个id,输入类型显然有一个名字(但没有 id)。到目前为止,我设法开始工作的唯一代码是:

$('input[name=my_name]:eq(1)').attr('checked', 'checked');

But I'd like to be able to check it by explicitly providing the value.

但我希望能够通过显式提供值来检查它。

回答by nickf

So you want to select the radio which has a particular value:

所以你想选择具有特定值的收音机:

$('input[name=my_name][value=123]').attr('checked', true); // or 'checked'

回答by anand

Below code worked with me, if I am assigning an ID to the radio button:

下面的代码对我有用,如果我为单选按钮分配一个 ID:

<input type="radio" id="rd_male" name="gender" value="Male" />
<input type="radio" id="rd_female" name="gender" value="Female" />
$('#rd_male').prop('checked', true);

回答by Moory Pc

$('input[name=field]:eq(1)').click();

Note : field = radio button name property

注意:字段 = 单选按钮名称属性

回答by Reza

You should use prop instead of using attr . It's more recommended

您应该使用 prop 而不是使用 attr 。比较推荐

    $('input[name=my_name][value=123]').prop("checked",true)

回答by Poorna

In order to select the radio button programmatically, you can call a jQuery trigger or $(".radioparentclass [value=radiovalue]")[0].click();

为了以编程方式选择单选按钮,您可以调用 jQuery 触发器或 $(".radioparentclass [value=radiovalue]")[0].click();