gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:39:40 24 4
gpt4 key购买 nike

好吧,我对这个一头雾水。

我有一个脚本,它从 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()
}
}
};

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

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

最佳答案

我猜 this is your problem

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

optionInput.defaultChecked = selected;

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

关于javascript - 复选框不会使用 Javascript 在 IE7 中检查,但没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1049396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com