javascript - 为什么我的checkbox默认选中在ajax返回的数据中判断不管用
ringa_lee
ringa_lee 2017-04-11 10:09:45
[JavaScript讨论组]
       <p class="form-group">
                                        <label class="control-label col-md-2">信息添加</label>
                                        <p class="col-md-10">
                                            <p 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>
                                            </p>
                                        </p>
                                    </p>
        $("#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

ringa_lee
ringa_lee

ringa_lee

全部回复(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);

再试试

PHP中文网
 $("#txtTXT").attr("checked", true);

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

为什么不试试:

$("#txtTXT").prop("checked", "checked");
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号