开发者社区> 问答> 正文

为什么我的checkbox默认选中在ajax返回的数据中判断不管用

  <div class="form-group">
                                        <label class="control-label col-md-2">信息添加</label>
                                        <div class="col-md-10">
                                            <div class="checkbox-list">
                                                <label class="checkbox-inline">
                                                    <input type="checkbox" value="txtDate" id="txtDate"/>
                                                    当前日期
                                                </label>
                                                <label class="checkbox-inline">
                                                    <input type="checkbox" value="txtPM25" id="txtPM25" />
                                                    PM2.5
                                                </label>
                                                <label class="checkbox-inline">
                                                    <input type="checkbox" value="txtTemper" id="txtTemper" />
                                                    当前温度
                                                </label>
                                            </div>
                                        </div>
                                    </div>
全选复制放进笔记        $("#zTree-cab").bind("cabZtreeOnClick", function (event, node) {
             _id = node.objID, _type = node.type, _name = encodeURI(node.name);
            if (_id == undefined || _id == null)
                _id = node.id;
            if (_type == 'LED') {

                $.ajax({
                    type: "Post",
                    dataType: "json",
                    url: "./BackHandler/DataManageHandler.ashx",
                    data: { page: "formledinfo", execute: "select", datas: _id },
                    beforeSend: function () {
                        Metronic.blockUI({
                            target: '#tab',
                            overlayColor: 'none',
                            animate: true
                        });
                    },
                    error: function (msg) {
                        alert("加载超时,可能网速比较慢!" + msg);
                    },
                    success: function (data) {
                        if (data!=null) {
                            for (var i = 0; i < data.length; i++) {
                                if (data[0].IsinputTxt) {
                                    $("#txtTXT").attr("checked", true);
                                }
                                if (data[0].IsTemper) {
                                    $("#txtTemper").attr("checked", true);
                                }
                                if (data[0].IsPM25) {
                                    $("#txtPM25").attr("checked", true);
                                }
                                if (data[0].IsCurrentDate) {
                                    $("#txtDate").attr("checked", true);
                                }
                                if (!data[0].BrightnessControl) {
                                    $("#DisplayType0").val(0);
                                } else {
                                    $("#DisplayType0").val(1);
                                }
                                $("#SlidingType").val(data[0].SlidingType);
                                if (!data[0].TemperatureSensor) {
                                    $("#TemperatureSensor").val(0);
                                } else {
                                    $("#TemperatureSensor").val(1);
                                }
                                $("#ip").val(data[0].VMSIP);

                                if (!data[0].Photocell) {
                                    $("#Photocell").val(0);
                                }
                                else {
                                    $("#Photocell").val(1);
                                }

                            }
                        }
                    }
                });
            }

        });

展开
收起
a123456678 2016-03-13 10:39:07 2614 0
1 条回答
写回答
取消 提交回答
  • 看你用的jquery版本,如果是1.8.3版本的话是没问题的,不过高于这个的话(比如1.9.0)则会返回undefined,
    具有true和false两个属性的属性最好用prop(),其他用attr();
    顺便贴一下1.8.3版本和1.9.0版本attr的区别,希望能够帮到你(prop是一致的)
    1.8.3 attr():

    boolHook = {
        get: function( elem, name ) {
            // Align boolean attributes with corresponding properties
            // Fall back to attribute presence where some booleans are not supported
            var attrNode,
                property = jQuery.prop( elem, name );
            return property === true || typeof property !== "boolean" && ( attrNode = elem.getAttributeNode(name) ) && attrNode.nodeValue !== false ?
                name.toLowerCase() :
                undefined;
        }
    }
    1.9.0 attr():
    
     boolHook = {
            get: function( elem, name ) {
                var
                    // Use .prop to determine if this attribute is understood as boolean
                    prop = jQuery.prop( elem, name ),
        
                    // Fetch it accordingly
                    attr = typeof prop === "boolean" && elem.getAttribute( name ),
                    detail = typeof prop === "boolean" ?
        
                        getSetInput && getSetAttribute ?
                            attr != null :
                            // oldIE fabricates an empty string for missing boolean attributes
                            // and conflates checked/selected into attroperties
                            ruseDefault.test( name ) ?
                                elem[ jQuery.camelCase( "default-" + name ) ] :
                                !!attr :
        
                        // fetch an attribute node for properties not recognized as boolean
                        elem.getAttributeNode( name );
        
                return detail && detail.value !== false ?
                    name.toLowerCase() :
                    undefined;
            }
        }
    2019-07-17 19:02:03
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载