通过循环从JSON中选择适当的单选按钮

时间:2016-10-06 22:26:14

标签: javascript jquery json

我收到了JSON,并且我正在使用相关数据填充表单的控件。 除了radio buttons之外,所有控件都可以正常工作。在我的数据库中,我将单选按钮的值保存为BIT

因此,我正在解析JSON并为每个控件循环并分配它的值。但是,似乎我在radio buttons(可能是case语句或我的html)上做错了。有什么想法吗?

  • 我使用的是jQuery 1.9

HTML

            <div class="col-sm-5">
                <label class="radio-inline">
                    <input type="radio" class="myform" value="1" name="testx">
                    YES
                </label>
                <label class="radio-inline">
                    <input type="radio" class="myform" value="0" name="testx">
                    NO
                </label>

            </div>

            <div class="col-sm-5">
                <label class="radio-inline">
                    <input type="radio" class="myform" value="1" name="highdanger">
                    YES
                </label>
                <label class="radio-inline">
                    <input type="radio" class="myform" value="0" name="highdanger">
                    NO
                </label>
            </div>

我的循环

     $.ajax({
         type: "GET",
         cache: false,
         url: serviceUrl + "/ModuleTask/GetExamination" + qstring,
        // beforeSend: sf.setModuleHeaders,
         contentType: "application/json; charset=utf-8"
     }).done(function (result) {
         var oResult = JSON.parse(result);

         // reset form values from json object
         $.each(oResult, function (name, val) {
             var $el = $('[name="' + name + '"]'),
                 type = $el.attr('type');

             switch (type) {
                 case 'checkbox':
                     $el.attr('checked', 'checked');
                     break;
                 case 'radio':
                     // $el.filter('[value="' + val + '"]').attr('checked', 'checked');
                     //$el.attr('checked', 'checked');
                    // $el.prop("checked", val);
                     //$el.filter('[value="' + val + '"]').prop("checked", true);
                    // $el.filter('[value="' + true + '"]').val(1);
                     $el.filter('[value="' + val + '"]').prop("checked", true);
                     break;
                 default:
                     $el.val(val);
             }
         });


     }).fail(function (xhr, result, status) {
         alert(result);
     });

AJAX响应

{"ExaminationID":1,"UserID":44,"PortalID":0,"bloodcholisterol":1,"bloodldl":2,"bloodhdl":3,"bloodtriglycerides":4,"bloodglucose":5,"bodyweight":6,"bodyheight":7,"bodycircumference":8,"bodyexaminer":"DD","bodycomments":"a1","pressurestarttime":null,"pressureone":100,"pressuretwo":200,"pressurethree":300,"pressureexaminer":"B","pressurecomments":"B1","FileID":null,"agogiartiriakipiesi":null,"agogilipidia":null,"agogisakxarodidiaviti":null,"examinationcomments":"c1","highdanger":false,"sistaseis":"no sistaseis","axiologisiexaminer":"XH"}

1 个答案:

答案 0 :(得分:1)

radio切换案例中,您需要先将boolean转换为整数。按此整数值过滤并将checked属性更改为true。尝试这样的事情......

case 'radio':
     var i = val ? 1 : 0;
     $el.filter("[value='"+ i +"']").attr("checked", true);
     break;

这是一个JSFiddle来演示。