为什么我的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);
                                }

                            }
                        }
                    }
                });
            }

        });

ajax返回回来的数据是没有问题的那几个checkbox返回的都是true

图片描述

阅读 4k
4 个回答

看你用的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;
        }
    }

操作checkbox,不要用

$("#txtTXT").attr("checked", true);

应该用

$("#txtTXT"). prop("checked", true);

再试试

 $("#txtTXT").attr("checked", true);

 id为txtTXT的这个元素在你的页面里我没有找到。看看是这个原因吗。

为什么不试试:

$("#txtTXT").prop("checked", "checked");
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题