Selectbox隐藏下拉列表中的第一个选项条目?

acc*_*man 2 html javascript forms jquery

是否可以隐藏第一个选项,即<option>Select One</option>当用户单击下拉框以查看选项时.因此,选择一个文本不会出现在列表中

Jam*_*ice 7

由于没有可靠的跨浏览器方式来隐藏option元素,最好的办法是将其删除focus并再次添加blur:

jQuery(document).ready(function () {
    var first = jQuery('#myselect').find('option').first();
    jQuery('#myselect').on('focus', function (e) {
        first.remove();
    }).on('blur', function (e) {
        jQuery(this).find('option').first().before(first);
    });
});
Run Code Online (Sandbox Code Playgroud)

这是一个有效的例子


小智 6

当select打开时,您可以使用纯HTML来隐藏第一个Element选项:

<select name="list">
    <option selected="selected" hidden>First Element</option>
    <option>Second Element</option>
    <option>Third Element</option>

</select>
Run Code Online (Sandbox Code Playgroud)

这是工作小提琴:https: //jsfiddle.net/sujan_mhrzn/y5kxtkc6/