JSON:选择的选项在IE中不工作,而在firefox中工作

JSON: selected option is not working in IE while works in firefox

本文关键字:工作 而在 firefox IE 选择 选项 JSON      更新时间:2023-09-26

我对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的任何问题

相关文章: