复选框不会使用Javascript检入IE7,但没有错误 [英] Checkboxes will not check in IE7 using Javascript, and yet no errors

查看:95
本文介绍了复选框不会使用Javascript检入IE7,但没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我对此感到非常困惑。

Okay I'm totally confused on this one.

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

I have a script that receives a bunch of values from a JSON object and creates a bunch of checkboxes and either checks or unchecks a these checkboxes based on their values.

此脚本对待我就像一个女人对待我...

This script treats me like a woman treats me...

如果你不知道发生了什么问题,那么我不会告诉你...

"If you don't know what's wrong, then I'm not going to tell you..."

脚本在IE8,Firefox3, etc ... etc ...

The script works correctly in IE8, Firefox3, etc... etc...

但是...

在IE7中,脚本无法检查复选框。它显示没有错误,从我可以告诉,脚本运行正常。我只是不检查任何复选框,我不知道为什么...

In IE7 the script fails to check off the checkboxes. It displays no errors and from what I can tell, the script runs just fine. I just doesn't check any of the checkboxes, and I don't know why...

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()
                                                }
                        }                           
};

function stockInit(){
alert(BEGIN:stockInit());
// TODO:你将收到一个on和一个off选项,
//一个将具有selected属性的selected,
//和另一个将具有selected属性
//
//具有
//的selected属性的选项将生成未选中的复选框。
//
//具有selected属性的选择
//的选项将生成一个被选中的复选框
//

//为什么?你问...因为这只是
//设置的方式。
for(var item in shoppingCart)
{
// // console.log(processing item:+ item);

function stockInit() { alert("BEGIN: stockInit()"); // TODO: You will recieve an "on" and an "off" option, // One will have a "selected" attribute of "selected", // and the other will have a "selected" attribute of "" // // The option that has the "selected" attribute of "" // will generate a checkbox that is not checked. // // The option that has the "selected attribute of "selected" // will generate a checkbox that is checked. //
// Why? You ask...because that's just the way the thing is // setup. for(var item in shoppingCart) { // // console.log("processing item: " + item);

        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()");
}

请不要问我为什么这样做。 。我真的可以告诉你,我没有访问后端代码...所以我得到了我得到的...

And please don't ask why I'm doing things this way...all I can really tell you is that I don't have access to the backend code...so I get what I get...

推荐答案

我想这是您的问题

基本上,解决方案是另外执行此操作:

basically the solution is to additionally do this:

optionInput.defaultChecked = selected;

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

or alternatively set the checked parameter after inserting the checkbox into the DOM

这篇关于复选框不会使用Javascript检入IE7,但没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆