复选框不会使用Javascript检查IE7,但没有错误

lee*_*d00 1 javascript checkbox json internet-explorer-7

好吧,我对这个完全感到困惑.

我有一个脚本从JSON对象接收一堆值并创建一堆复选框,并根据它们的值检查或取消选中这些复选框.

该脚本在IE8,Firefox3等中正常工作......等等......

然而...

在IE7中,脚本无法选中复选框.它没有显示错误,据我所知,脚本运行得很好.我只是没有检查任何复选框,我不知道为什么...

shoppingCart['Update_Stock_Item_0_NRD%5FHAT2'] = {
            'propeller': {
                            'label'          : 'propeller',                             
                            'optionValues'   : {                                                    
                                                 'on' : {
                                                    'selected': 'selected'
                                                        },
                                                  'off' : {
                                                            'selected': ''
                                                          },
                                                        '' : new String()
                                                }
                        },
            'sunLogo': {
                            'label'          : 'sunLogo',                           
                            'optionValues'   : {                                                    
                                                 'on' : {
                                                    'selected': 'selected'
                                                        },
                                                  'off' : {
                                                            'selected': ''
                                                          },
                                                        '' : new String()
                                                }
                        },
            'MSLogo': {
                            'label'          : 'sunLogo',
                            'optionValues'   : {                                                    
                                                 'on' : {
                                                    'selected': 'selected'
                                                        },
                                                  'off' : {
                                                            'selected': ''
                                                          },
                                                        '' : new String()
                                                }
                        }                           
};
Run Code Online (Sandbox Code Playgroud)

function stockInit(){alert("BEGIN:stockInit()"); // TODO:你会收到一个"on"和一个"off"选项,//一个将有一个"selected"属性为"selected",//另一个将有一个"selected"属性为""// //具有"选择"属性""//的选项将生成未选中的复选框.// //选中"选定属性"选项"//将生成一个选中的复选框.//
//为什么?你问......因为这就是设置//的方式." (shoppingCart中的var项目){// // console.log("处理项目:"+项目);

        var optionContainer = document.getElementById(item + "_optionContainer");

        for(var option in shoppingCart[item])
        {
            if(option != "blank")
            {
                // // console.log("option: " + option);

                var currentOption = shoppingCart[item][option]['optionValues'];

                // // console.log("currentOption['on']['selected']: " + currentOption['on']['selected']);
                // // console.log("currentOption['off']['selected']: " + currentOption['off']['selected']);

                // Really you only have to check the one, but just to be through-o
                var selected = (currentOption['on']['selected'] == 'selected') ? true : false;
                selected = (currentOption['off']['selected'] == 'selected') ? false : true;

                var label = document.createElement("LABEL");
                var labelText = document.createTextNode(shoppingCart[item][option]['label']);
                var optionInput = document.createElement("INPUT");

                var hiddenInput = document.createElement("INPUT");

                optionInput.setAttribute("type", "checkbox");
                optionInput.checked = selected;

                optionInput.setAttribute("id", option);
                alert(optionInput.id);
                alert(optionInput.checked);

                hiddenInput.setAttribute("type", "hidden");
                hiddenInput.setAttribute("name", option);
                hiddenInput.setAttribute("id", option + "_hiddenValue");
                hiddenInput.setAttribute("value", (optionInput.checked) ? "on" : "off");

                label.appendChild(optionInput);
                label.appendChild(labelText);
                label.appendChild(hiddenInput);

                (function(id)
                {
                    optionInput.onclick = function() {

                        var hiddenInput = document.getElementById(id + "_hiddenValue");

                        hiddenInput.setAttribute("value", (this.checked == true) ? "on" : "off");
                        alert("this.id: " + this.id);
                        alert("this.checked: " + this.checked);
                    }
                })(optionInput.id);

                optionContainer.appendChild(label);


            }
        }
        // // console.log("processing item of " + item + " complete");
    }
    alert("END: stockInit()");
}
Run Code Online (Sandbox Code Playgroud)

并且请不要问我为什么这样做...所有我能真正告诉你的是我无法访问后端代码......所以我得到的东西......

小智 13

我猜这是你的问题

基本上解决方案是另外这样做:

optionInput.defaultChecked = selected;
Run Code Online (Sandbox Code Playgroud)

或者在将复选框插入DOM后设置checked参数