2013-07-04 46 views
0

我想通過他的值選擇一個輸入。 問題是,我得到了4個無線電輸入 ,當用戶點擊「下一個」時,它將該答案的值保存在AnsW數組中。 當用戶點擊「返回」時,我希望輸入單選按鈕的值與我在AnsW數組中的值一致,因此用戶可以知道他已選擇了什麼,並將其答案更改爲其他內容。如何設置通過.val()檢查輸入的無線電?

的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"}) 
     } 
    }); 
} 
+0

做ü有單獨的頁面,這些問題嗎?或者問題是動態的?上[的jsfiddle] – krishgopinath

回答

4

jQuery selectors讓您根據自己的屬性,完全匹配例如,要查找元素:

$("element.class[attribute=value]") 

檢查選擇器中的值屬性

$("input[value="+tempAns+"]").prop('checked', true) 
+0

示例(http://jsfiddle.net/7LFKq/2/)(喬回答比我更快) – furas

+0

的更詳細的小提琴:http://jsfiddle.net/hungerpain/vhfTg/2/ – krishgopinath

+0

和使用'prop'。 – krishgopinath

相關問題