Options added to a <select> are invisible in the list box on iPhone

本小妞迷上赌 提交于 2019-12-10 11:27:27

问题


I have an HTML listbox and I'm adding options using javascript - no problems in any browsers on PC - Mozilla, Chrome, IE, Opera, Safari. However, when performed on iPhone the added option is invisible in the listbox -shows only - (0 items) until you tap on the listbox and then you see the added option as if you were tapping on a regular "select" item, tap on it and only after that the option is visible. I hope I made myself clear enough...

The functionality is as follows: user fills text in textbox (tb), clicks the add button (btn), the option is created and displayed in the listbox (lstbx)

Here's the code:

<input type="text" id="tb">

<select id="lstbx" multiple="multiple"></select>

<input type="button" id="btn" onclick="add_option(lstbx, form.tb, form.btn)">


function add_option(lstbx, tb, btn) {
    var option = document.createElement("option");
        if (tb.value != "") {
            option.text = tb.value;
            option.value = tb.value;
            lstbx.options.add(option);
            tb.value = "";}         
    }

回答1:


I have successfully used the options array to do this. So instead of creating HTML nodes, use the JS API. Also be aware that multi select boxes are not supported on iOS.

var opt = new Option(tb.value, tb.value, false, false);
lstbx.options.add(opt);


来源:https://stackoverflow.com/questions/11364040/options-added-to-a-select-are-invisible-in-the-list-box-on-iphone

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