javascript - JSON:所选选项在 IE 中不起作用,而在 Firefox 中有效

标签 javascript json internet-explorer

我对 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/

相关文章:

json - Go Unmarshal JSON,但将嵌套结构解码为字符串

json - Flutter 从复杂的 json 中获取数据

javascript - 如何从父html通过iframe传递参数?

javascript - 为什么 console.log() 在 Promises 的resolve() 函数中立即执行?

javascript - 悬停并单击图像以显示内容(显示初始内容)

javascript - 处理IE浏览器的关闭按钮

html - 为什么在 FF 而不是 IE 中这些 float 的 Div 之间有额外的 "white space"?

javascript - 如何检测旧版本的浏览器并重定向到浏览器支持页面

php - 登录后在同一 Activity 中获取用户属性的 JSON 数组

internet-explorer - Internet Explorer 6 测试