1

我有一段代码可以使用单个主复选框(用作切换)来选择/取消选择所有 Dojo 复选框,该复选框在 firefox 上运行良好,但在 IE8 上却不行。我努力寻找问题,但一无所知,任何人都可以帮忙。我附上下面的代码:

    <script type="text/javascript">
    dojo.require("dojo.parser");

    dojo.ready(function() {
        var checkboxes = [];
        dojo.query('#waferCheck_cb input[type=checkbox]').forEach(function(node, index, arr){
        checkboxes.push(arr[index].id);
        var handle = dojo.connect(dijit.byId(arr[index].id), "onchange", function(evt){

            var cbClicked = evt.target.id;
            var cbStatus = dijit.byId(cbClicked).get("checked");
            setCBSelection(checkboxes,cbClicked,cbStatus );
            dojo.disconnect(handle);        
            });
        });
    });

    function setCBSelection(checkboxes,cb_Clicked, cb_Status) {
        var len = checkboxes.length;
        if(len > 0) {
            // get index of the checkbox clicked
            var cb_pos = 0;
            for(var i = 0; i < len; i++) {
                if(cb_Clicked == checkboxes[i]) {
                    cb_pos = i;
                    break;
                }
            }

            // If Select All checkbox clicked, set the other checboxes accordingly
            if(cb_pos == 0) {
                for(var i = 1; i < len; i++) {
                    dijit.byId(checkboxes[i]).set("checked", cb_Status);
                }
            } else {
                // If any other checkbox is clicked, set the Select All accordingly
                var allCBSameStatus = true;
                for(var i = 1; i < len; i++) {
                    var curCBStatus = dijit.byId(checkboxes[i]).get("checked");
                    if(curCBStatus != cb_Status) {
                        allCBSameStatus = false;
                        break;
                    };
                }
                if(allCBSameStatus){
                    dijit.byId(checkboxes[0]).set("checked", cb_Status)
                }else{
                    if(cb_Status == false) {
                        dijit.byId(checkboxes[0]).set("checked", cb_Status)
                    }
                }
            }
        }
    }
</script>
4

2 回答 2

0

I believe IE8 doesn't like missing semicolons. I see 2 lines that are missing semicolons.

dijit.byId(checkboxes[0]).set("checked", cb_Status)
于 2012-11-08T11:55:39.243 回答
0

function setCBSelection(...)之前是否尝试过定义dojo.ready(..)(见下文)?也许这会有所帮助,因为它是在定义之前使用的?如果这没有帮助,如果您发布某种错误消息 IE 可能会抛出它可能会很有用......

    dojo.require("dojo.parser");

    function setCBSelection(checkboxes,cb_Clicked, cb_Status) {
        var len = checkboxes.length;
        if(len > 0) {
            // get index of the checkbox clicked
            var cb_pos = 0;
            for(var i = 0; i < len; i++) {
                if(cb_Clicked == checkboxes[i]) {
                    cb_pos = i;
                    break;
                }
            }

            // If Select All checkbox clicked, set the other checboxes accordingly
            if(cb_pos === 0) {
                for(i = 1; i < len; i++) {
                    dijit.byId(checkboxes[i]).set("checked", cb_Status);
                }
            } else {
                // If any other checkbox is clicked, set the Select All accordingly
                var allCBSameStatus = true;
                for(i = 1; i < len; i++) {
                    var curCBStatus = dijit.byId(checkboxes[i]).get("checked");
                    if(curCBStatus != cb_Status) {
                        allCBSameStatus = false;
                        break;
                    }
                }
                if(allCBSameStatus){
                    dijit.byId(checkboxes[0]).set("checked", cb_Status);
                }else{
                    if(cb_Status === false) {
                        dijit.byId(checkboxes[0]).set("checked", cb_Status);
                    }
                }
            }
        }
    }


    dojo.ready(function() {
        var checkboxes = [];
        dojo.query('#waferCheck_cb input[type=checkbox]').forEach(function(node, index, arr){
        checkboxes.push(arr[index].id);
        var handle = dojo.connect(dijit.byId(arr[index].id), "onchange", function(evt){

            var cbClicked = evt.target.id;
            var cbStatus = dijit.byId(cbClicked).get("checked");
            setCBSelection(checkboxes,cbClicked,cbStatus );
            dojo.disconnect(handle);        
            });
        });
    });

顺便说一句:您应该始终使用===0或进行比较false...您可以使用jsFiddle 让 JSLint 检查您的代码,从而发现许多一开始没有看到的问题。希望我能帮助你。

于 2012-11-09T07:25:41.950 回答