我对 JSON 有以下查询:所选选项在 IE 中不起作用,而在 Firefox 中有效。
我有这样的示例数据:
var columnDefs = [...
{"name":"childPerformancePrice", "label":"Cosell Price", "type":"int", "control":"select", "options":performancePrices, "align":"left", "default":"", "required":false,"size": 6},
...]
性能下拉列表如下:
function getPerformancePrices(){
......
$.getJSON("?data=performancePrices", function(list) {
performancePrices.push([0, ""]);
$.each(list, function(index, item) {
performancePrices.push([item.id, item.description]);
performancePrices.sort();
});
...
});
}
示例 JSON 数据,如 JSON.stringify(columnDefs[index])
:
{"name":"childPerformancePrice", "label":"Cosell Price", "type":"int", "control":"select", "options":[[0,""],[15000,"Band 1"],[15001,"Band 2"],[15002,"Band 3"]],"align":"left", "default":"", "required":false,"size": 6}
问题:为什么在 IE 中编辑时下面选择的选项不起作用(即,在 IE 中选择不正确)而在 Firefox 中运行良好?
function selectCell(oColumnDef, value) {
var oSelect = createNamedElement("select", oColumnDef["name"]);
if (value == undefined) {
value = "";
}
$.each(oColumnDef["options"], function(index, item) {
var oOption = document.createElement("option");
oOption.value = item[0];
oOption.text = item[1];
if (item[1] == value) {
oOption.selected = true;
}
oSelect.options.add(oOption);
});
最佳答案
我唯一能想到的是,因为它在 FF 中有效,但在 IE 中无效,所以你创建这些选项的方式有些东西是后者不喜欢的。由于您已经在使用 jQuery,请尝试更改此设置:
var oOption = document.createElement("option");
oOption.value = item[0];
oOption.text = item[1];
if (item[1] == value) {
oOption.selected = true;
}
oSelect.options.add(oOption);
对此:
var oOption = $("<option />", { "value": item[0],
"text": item[1],
"selected": item[1] === value
});
$(oSelect).append(oOption);
假设 jQuery 将克服 IE 的任何怪癖。
关于javascript - JSON:所选选项在 IE 中不起作用,而在 Firefox 中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8571427/