JSON:当在Firefox中工作时,选择的选项在IE中不起作用

时间:2011-12-20 06:03:17

标签: 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);
    });

1 个答案:

答案 0 :(得分:1)

我唯一能想到的是,因为它适用于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的任何怪癖。