2011-12-20 51 views
4

我对JSON有以下查询:选择的选项不工作在IE浏览器,而在Firefox中工作。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

我能想到的唯一的事情就是,因为它在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); 

与任何怪癖IE浏览器,前提是jQuery将铁过来。

+0

仅供参考,提问者试图在编辑中询问后续问题;看[这里](http://stackoverflow.com/suggested-edits/163337)。 – 2011-12-20 11:23:48

相关问题