JSON: the selected option does not work in IE, but works in firefox

I have the following request in JSON: the selected option does not work in IE, working in firefox.

I have an example of data such as:

var columnDefs = [... {"name":"childPerformancePrice", "label":"Cosell Price", "type":"int", "control":"select", "options":performancePrices, "align":"left", "default":"", "required":false,"size": 6}, ...] 

Performance dropdown list, for example:

 function getPerformancePrices(){ ...... $.getJSON("?data=performancePrices", function(list) { performancePrices.push([0, ""]); $.each(list, function(index, item) { performancePrices.push([item.id, item.description]); performancePrices.sort(); }); ... }); } 

For example, JSON data such as 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} 

Question: why the parameter selected below does not work during editing (i.e. is not correctly selected in IE) in IE, works well in 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); }); 
+4
source share
1 answer

The only thing I can think of is that, since it works in FF, but not in IE, there is something about how you create these parameters that the latter do not like. Since you are already using jQuery, try changing this:

 var oOption = document.createElement("option"); oOption.value = item[0]; oOption.text = item[1]; if (item[1] == value) { oOption.selected = true; } oSelect.options.add(oOption); 

For this:

 var oOption = $("<option />", { "value": item[0], "text": item[1], "selected": item[1] === value }); $(oSelect).append(oOption); 

With the assumption that jQuery will catch any quirks of IE.

+1
source

Source: https://habr.com/ru/post/1387151/


All Articles