[英]how to set radio checked of an input by .val()?
我想通過他的值選擇一個輸入。 問題是我有4個無線電輸入,當用戶單擊“下一步”時,它將答案的值保存在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選擇器允許您根據元素的屬性來查找元素,這是一個完全匹配的示例:
$("element.class[attribute=value]")
檢查選擇器中的value屬性
$("input[value="+tempAns+"]").prop('checked', true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.