2016-07-16 87 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>