[英]Unable to get value from single radio button in group of radio buttons
I have a yes / no radio button combination that both have the name 'Rigid' and what I'm trying to do is loop over them and grab the the value to match it to a database value. 我有一个“是/否”单选按钮组合,它们都具有“ Rigid”(刚性)名称,而我想做的是在它们上循环并获取值以使其与数据库值匹配。 I can get the DB value ok but I don't seem to be able to get the radio button value.
我可以确定数据库值正常,但似乎无法获取单选按钮值。
Here's the JQuery I'm using: 这是我正在使用的JQuery:
var rigid = $("[name='Rigid']");
for (var j = 0; j < rigid.length; j++) {
var selValueRigid = rigid[j].val();
if (selValueRigid === data.Rigid) {
rigid[j].prop("checked", true);
} else {
rigid[j].prop("checked", false);
}
}
And the generated HTML: 并生成HTML:
<label for="RigidYes">
<i class="fa fa-check-circle active"></i>
<i class="fa fa-circle-thin inactive"></i>Yes
</label>
<input name="Rigid" id="RigidNo" value="F" checked="checked" type="radio">
<label for="RigidNo">
<i class="fa fa-check-circle active"></i>
<i class="fa fa-circle-thin inactive"></i>No
</label>
You can directly use 您可以直接使用
$('[name="Rigid"][value="' + data.Rigid + '"]').prop('checked', true);
However as per problem statement, use .eq(index)
method to get object at specified index. 但是,根据问题陈述,使用
.eq(index)
方法获取指定索引处的对象。
var el = rigid.eq(j);
el.prop("checked", el.val() === data.Rigid);
Problem with your implementation is that []
will return you reference of native DOM element which doesn't have .val()
and .prop()
method. 实现的问题是
[]
将返回您对没有.val()
和.prop()
方法的本机DOM元素的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.