复选框不会使用Javascript签入IE7,但不会出现错误
好吧,这件事我完全搞糊涂了 我有一个脚本,它从JSON对象接收一组值,并创建一组复选框,然后根据它们的值选中或取消选中这些复选框 该脚本在IE8、Firefox3等中正常工作。。。等等 然而 在IE7中,脚本无法勾选复选框。它没有显示任何错误,从我所知道的,脚本运行得很好。我只是不勾选任何复选框,我不知道为什么复选框不会使用Javascript签入IE7,但不会出现错误,javascript,json,checkbox,internet-explorer-7,Javascript,Json,Checkbox,Internet Explorer 7,好吧,这件事我完全搞糊涂了 我有一个脚本,它从JSON对象接收一组值,并创建一组复选框,然后根据它们的值选中或取消选中这些复选框 该脚本在IE8、Firefox3等中正常工作。。。等等 然而 在IE7中,脚本无法勾选复选框。它没有显示任何错误,从我所知道的,脚本运行得很好。我只是不勾选任何复选框,我不知道为什么 shoppingCart['Update_Stock_Item_0_NRD%5FHAT2'] = { 'propeller': { 'label': 'propeller',
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: 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()");
}
请不要问我为什么这样做…我能告诉你的是我没有访问后端代码的权限…所以我得到了我得到的…我想
基本上,解决方案是另外执行以下操作:
optionInput.defaultChecked = selected;
或者在将复选框插入DOM后设置checked参数是否尝试设置“optionInput.checked=true;”以查看是否有任何更改?建议使用一个新标记:whitespace madness。说真的,试着用少于113个空格缩进。呵呵,有趣。。。我想说“set.checked在它被附加到DOM之后”,它看起来应该是有效的。。。但无论如何,在这种情况下,他不能真正做到这一点,因为这是在一个环形井内。我想不久前我也遇到过类似的问题,是的,就是这样:)