我想通过他的值来选择一个输入。 问题是我有 4 个 radio 输入 当用户单击“下一步”时,它将将该答案的值保存在 AnsW 数组中。 当用户单击“后退”时,我希望检查输入单选按钮及其 AnsW 数组中的值,以便用户可以知道他选择了什么并将其答案更改为其他内容。
html代码:
<ul>
<li class="li0">
<input type="radio" value="ch1" name="choice" id="li0"/>
<label for="li0"></label>
</li>
<li class="li1">
<input type="radio" value="ch2" name="choice" id="li1"/>
<label for="li1"></label>
</li>
<li class="li2">
<input type="radio" value="ch3" name="choice" id="li2"/>
<label for="li2"></label>
</li>
<li class="li3">
<input type="radio" value="ch4" name="choice" id="li3"/>
<label for="li3"></label>
</li>
</ul>
我的代码是:
function go_back(){
$("#back").bind("click",function(){
qNum--;
showQuestion(qNum);
if(AnsW.length === 0){
calcAnswers--;
}
alert(AnsW[qNum]);
tempAns = AnsW[qNum];//user last answer which is false i need this to make the radio button point to that answer
//alert( $("input[value='tempAns']"));
$("input").val(tempAns).attr('checked', true);
alert(tempAns);
//alert(tempAns);
AnsW.splice(-1,1);//delete the false answer from the array i need this to make sure that the answer is deleted and there wont be overload of wrong answers
//alert(deleteAns);
if(qNum === 0){
$("#back").css({"visibility":"hidden"})
}
});
}
最佳答案
jQuery selectors允许您根据元素的属性查找元素,精确匹配示例:
$("element.class[attribute=value]")
检查选择器中的 value 属性
$("input[value="+tempAns+"]").prop('checked', true)
关于javascript - 如何设置 .val() 对输入进行单选检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17460494/