简体   繁体   English

使用jquery中的值设置单选按钮

[英]Set Radio Button using value in jquery

How to set Radio button checked(select) using Attribute value 如何使用属性值设置单选按钮(选择)

html : HTML:

<div class="controls">
    <input type="radio" id="chairs_canadd" name="chairs_canadd" value="1" />
    <input type="radio" id="chairs_canadd" name="chairs_canadd" value="0" />
</div>

now im getting the value of chairs_canadd value from database via AJAX. 现在我通过AJAX从数据库中获取chairs_canadd值。 if the value is 0 then the second have to get selected , if the value is 1 then first one has to be get selected. 如果值为0则必须选择第二个,如果值为1,则必须先选择第一个。

Firstly, your IDs are not unique. 首先,您的ID不是唯一的。

Second, a radio button group like this can only have one checked value at a time. 其次,像这样的单选按钮组一次只能有一个检查值。 So why not just use .val : 那么为什么不使用.val

$('input:radio[name=chairs_canadd]').val([editValue.chairs_canadd]);

jsFiddle 的jsfiddle

You have value for option that needs to be set in variable chairs_canadd . 您有需要在变量chairs_canadd设置的选项的值。 you can get the radio button by value and then set it. 您可以按值获取单选按钮然后进行设置。 Like this: 像这样:

$(".controls input[value="+chairs_canadd+"]").prop("checked", true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM