【发布时间】:2016-07-28 09:45:30
【问题描述】:
我需要一个帮助。我需要使用 Javascript/Jquery 动态检查单选按钮。我在下面解释我的代码。
<div style="float:left; margin-right:10px;">
<input type="radio" name="answer_type0" id="answer_type0" onclick="selectScale(this.value,'0');" value="5736db13ed8cda709ffc8918">Scale
<input type="radio" name="answer_type0" id="answer_type0" onclick="selectScale(this.value,'0');" value="5736daffed8cda709ffc8917">Yes/No
<input type="radio" name="answer_type0" id="answer_type0" onclick="selectScale(this.value,'0');" value="5736db29ed8cda709ffc8919">Written
</div>
<span>
<button type="button" id="btn" name="btn" onclick="setRadioButtonValue()">Set</button>
</span>
这里有 3 个单选按钮可用。当用户单击设置按钮时,应根据给定值检查一个单选按钮。我的 javascript 代码如下所示。
function setRadioButtonValue(){
var valu="5736daffed8cda709ffc8917";
$('#answer_type0').val(valu).prop('checked', true);
console.log('check',document.getElementById('answer_type0').checked);
}
我的更新代码在这里可用
在这里我需要根据给定的值(i.e-var valu="5736daffed8cda709ffc8917")进行检查,以便根据代码检查Yes/No,但在我的情况下,始终检查第一个单选按钮。请帮我解决这个问题。类似地我还需要检查每组中的多组单选按钮。我的plunkr 代码在这里。
【问题讨论】:
-
为什么要为多个单选按钮提供相同的 id "answer_type0"?
-
@MayankPandey :因为我从他们那里得到了一个值。
-
函数
selectScale在哪里? -
它的一个函数会在检查时调用。
标签: javascript jquery html