简体   繁体   English

Chrome浏览器:将选项添加到选择框不会显示在下拉列表中

[英]Chrome Browser: Adding Options to a Select Box doesn't show in dropdownlist

When a user clicks on a selectbox, an Ajax even occurs. 当用户单击选择框时,甚至会发生Ajax。

This event places options in to the select box. 此事件将选项放入选择框。

After the options are placed in to the select box the user should be able to click on them. 将选项放入选择框后,用户应该可以单击它们。

In Chrome, when you click the drop down list appears "Select One" but after the options are added, the list still is "Select One" the user can hit the down arrow key and select it, but it is not shown in the "drop down list of options" after they are added. 在Chrome浏览器中,当您单击下拉列表时,会显示“选择一个”,但添加选项后,列表仍为“选择一个”,用户可以单击向下箭头键并选择它,但不会显示在“下拉选项”。

What magical piece of JS or jQuery would refresh this drop down list being displayed without a second click in chrome? 哪个神奇的JS或jQuery片段将刷新显示的下拉列表,而无需再次单击chrome?

When the option is clicked this is being executed via JS: 单击该选项后,将通过JS执行该操作:

secBox = document.getElementById('sel34541_75');
secBox.length = 1;
addOption(secBox, "Option 1", "execPopDrop('sel34541_75', 34541, 'Option 1', 'type1', 'page.php')");
addOption(secBox, "Option 2", "execPopDrop('sel34541_75', 34541, 'Option 2', 'type2', 'page.php')");
addOption(secBox, "Option 3", "execPopDrop('sel34541_75', 34541, 'Option 3', 'type3', 'page.php')");
addOption(secBox, "Option 4", "execPopDrop('sel34541_75', 34541, 'Option 4', 'type4', 'page.php')");

Add option function looks like this: 添加选项功能如下所示:

function addOption(selectbox, text, value) {
  selectbox.options[selectbox.options.length]=new Option(text,value);
}

If i click and then hit the down arrow, I see the dynamic options are there, but not in the list visible. 如果单击然后单击向下箭头,则可以看到动态选项,但看不到列表中的动态选项。 (again this works in IE and FF) (同样适用于IE和FF)

如果我按下向下箭头,我会看到动态选项

If I click it a second time I see the correct options in the drop down 如果我第二次单击它,则在下拉列表中会看到正确的选项

如果第二次单击它,则在下拉列表中会看到正确的选项

Try this - works in all browsers - if you need support for IE4, then you need to add 1 to the options.length before adding the option ;) 尝试一下-适用于所有浏览器-如果需要IE4支持,则需要在options.length中添加1,然后再添加option;)

update this solution is to the original question 更新此解决方案是针对原始问题

function addOption(selectbox, text, value) {
  selectbox.options[selectbox.options.length]=new Option(text,value);
}

However WHY add that horrible string to the option? 但是,为什么要添加该可怕的字符串到选项?

Looks like you would (shudder) EVAL the string in the value 看起来您会(颤抖)对值中的字符串进行EVAL

Why not 为什么不

addOption(secBox, "Option 1", "sel34541_75");

and then in execPopDrop pass the select, then it would be 然后在execPopDrop中传递选择,那么它将是

function execPopDrop(sel) {
  var val = sel[sel.selectedIndex].value;
  var txt = sel[sel.selectedIndex].text;
  var type="type"+val.split(" ")[1]; // from Option 1
  var parm2 = val.replace(/[sel_]/g,""); // 34541
  // here you have all the stuff you need to do whatever you did before
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM