Why does IE not add <option> to my dropDown?

让人想犯罪 __ 提交于 2019-12-02 13:25:39

There's a much simpler, and more cross-browser friendly, way to add options to select boxes than adding option DOM elements (live example):

$button.click(function() {
    var option = new Option('hello');
    $select[0].options.add(option);
});

Perhaps that would work more reliably for you. (Note that it's add, not push. The options object on select elements isn't really an array.)

Instead of options.add you can also do:

$button.click(function() {
    var options = $select[0].options,
        option  = new Option('hello');
    options[options.length] = option;
});

...which also adds to the end, in an array-like way. But add is reliable cross-browser.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!