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

如何在使用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;
}
  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程