2016-07-16 24 views
1

我需要一個幫助。我需要檢查單選按鈕並使用Javascipt/Jquery設置它的值。我正在下面解釋我的代碼。無法使用JavaScript或Jquery檢查單選按鈕

<input type="radio" name="answer_type0" id="answer_type0" value="" onClick="selectScale();">Male 
    <button type="button" id="btn">Edit</button> 
    <script > 
     document.getElementById('btn').onclick=function(){ 
      var qdata='123'; 
      $('#answer_type0[value="' + qdata + '"]').prop('checked', true).trigger('click'); 
      //$('#answer_type0').prop("checked",true); 
      console.log('checked',$("#answer_type0").prop("checked")); 
     } 
     function selectScale(){ 
      console.log('value'); 
     } 
    </script> 

在上面的代碼,當用戶點擊按鈕Edit單選按鈕應檢查和click應觸發。但在我看來,並沒有像這樣發生。 Here plunkr存在。請幫助我。

+0

你能說得對嗎? – subhra

+1

在選擇器中,您期望輸入具有值屬性,而不是。 – gcampbell

+0

檢查此[演示](https://jsfiddle.net/ya8z86s7/) – guradio

回答

1

這裏是回答

<input type="radio" name="answer_type0" id="answer_type0" value="" onClick="selectScale();">Male 
<button type="button" id="btn">Edit</button> 
<script> 
    document.getElementById('btn').onclick = function() { 
    var qdata = '0'; 
    $('[name=answer_type' + qdata + ']').prop('checked', true).trigger('click'); 
    console.log('checked', $("#answer_type0").prop("checked")); 
    } 

    function selectScale() { 
    var qdata = '0'; 
    $('[name=answer_type' + qdata + ']').val('some_value'); 
    console.log('some_value'); 
    } 

</script> 
+0

讓我試試你的代碼。 – subhra

+0

請在我的plunkr裏面測試。 – subhra

+0

不工作?你能解釋一下沒有用嗎?這裏是jsfiddle:https://jsfiddle.net/qLyx07ch/ –

0

嘗試這樣的。

<!DOCTYPE html> 
<html> 

    <head> 
    <meta charset="utf-8" /> 
    <title></title> 
    <link rel="stylesheet" href="style.css" /> 
    <script data-require="jquery" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script> 
    </head> 
    <body> 
    <input type="radio" name="answer_type0" id="answer_type0" value="" onClick="selectScale();">Male 
    <button type="button" id="btn">Edit</button> 
    <script > 
     $(document).ready(function(){ 
      document.getElementById('btn').onclick=function(){ 

      $('#answer_type0').prop("checked",true); 
     } 
     function selectScale(){ 
      console.log('value'); 
     } 
     }); 
    </script> 
    </body> 

</html> 
相關問題