dpafea04148
2016-10-06 18:56
浏览 47
已采纳

如何在使用jQuery编辑时设置单选按钮的值?

I've a radio button in my form and when I enter a reference number, I can fetch the corresponding form details along with source value using jQuery. I don't know how to set that value to radio button. Thank You!

<input type="radio" ng-model="selected" name="source" class="source" <?php echo ($row['source']=='VAT')?'checked':'' ?> value="VAT">VAT
<input type="radio" ng-model="selected" name="source" class="source" <?php echo ($row['source']=='CST')?'checked':'' ?> value="CST">CST

jQuery

$('#gp_no').on('keypress', function(e){
    if(e.keyCode === 13)
    {
        $(this).trigger("enterKey");
        var val = $(this).val();
        var dataString = "gp_no=" + val;
        $.ajax({
            type: "POST",
            url: "post_process.php",
            data: dataString,
            dataType: 'json',
            success: function(data){
                $('.source').val(data.source);
            }
        });
    }
}); 

post_process.php

if(ISSET($_POST['gp_no'])) {

    $gp_no = $con->real_escape_string($_POST['gp_no']);

    $query = "SELECT * FROM items WHERE items.gp_no = $gp_no";

    if ($result = $con->query($query)) {
        $row = $result->fetch_assoc();
        $source = $row['source'];
    }
    $json = array($vehicle_no, 'source' => $source);
    $json = json_encode($json);
    echo $json;
}

图片转代码服务由CSDN问答提供 功能建议

我的表单中有一个单选按钮,当我输入参考编号时,我可以获取相应的表单详细信息 以及使用 jQuery 来源值。 我不知道如何将该值设置为单选按钮。 谢谢!

 &lt; input type =“radio”ng-model =“selected”name =“source”class =“source”&lt;?php echo($ row)  ['来源'] =='增值税')?'已检查':''?&gt;  value =“VAT”&gt; VAT 
&lt; input type =“radio”ng-model =“selected”name =“source”class =“source”&lt;?php echo($ row ['source'] =='  CST')?'已检查':''?&gt; 值= “CST” &GT; CST 
   
 
 

的jQuery

 <代码> $( '#gp_no')上(  'keypress',function(e){
 if(e.keyCode === 13)
 {
 $(this).trigger(“enterKey”); 
 var val = $(this).val(  ); 
 var dataString =“gp_no =”+ val; 
 $ .ajax({
 type:“POST”,
 url:“post_process.php”,
 data:dataString,
 dataType:'  json',
 success:function(data){
 $('。source')。val(data.source); 
} 
}); 
} 
});  
   
 
 

post_process.php

  if(ISSET($ _ POST ['gp_no'])){
  
 $ gp_no = $ con&gt; real_escape_string($ _ POST ['gp_no']); 
 
 $ query =“SELECT * FROM items WHERE items.gp_no = $ gp_no”; 
 
 if($ result  = $ con&gt;查询($ query)){
 $ row = $ result-&gt; fetch_assoc(); 
 $ source = $ row ['source']; 
} 
 $ json = array(  $ vehicle_no,'source'=&gt; $ source); 
 $ json = json_encode($ json); 
 echo $ json; 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutu9810 2016-10-06 19:06
    已采纳

    If you are looking to get the value of a selected radio button you can do this:

    $('input[name="source"]:checked').val()
    

    When you get the response from your ajax call you check your button:

    First change this:

    <input type="radio" ng-model="selected" name="source" class="source" id="VAT" value="VAT">VAT
    <input type="radio" ng-model="selected" name="source" class="source" id="CST" value="CST">CST
    
    $("#"+data.source).prop("checked", true)
    

    This assune that your returned value is VAT or CST

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题