简体   繁体   English

如何防止显示 html select(下拉)框选项

[英]how to prevent html select (dropdown) box options from displaying

I was trying to attach to the onclick and the onchange events, but haven't met with a lot of success.我试图附加到 onclick 和 onchange 事件,但没有取得很大的成功。

The ultimate goal is to overlay a div or a ul and display the options where the user can click;最终目标是覆盖一个 div 或一个 ul 并显示用户可以点击的选项; this will then change the option on the select box.这将更改 select 框上的选项。 Is it possible to do this?是否有可能做到这一点?

I've tried out some code here.我在这里尝试了一些代码。 The non-working example can be found below:可以在下面找到非工作示例:

http://jsfiddle.net/deostroll/Yed7p/1/ http://jsfiddle.net/deostroll/Yed7p/1/

The markup for the dropdown:下拉菜单的标记:

<select>
    <option value="000">Select A Country</option>
    <option value="001">India</option>    
    <option value="002">Australia</option>    
    <option value="003">United States Of America</option>
    <option value="004">Great Britan</option>
    <option value="005">Zimbabwe</option>
    <option value="006">Newzeland</option>    
</select>

Here is the javascript:这是 javascript:

$(function(){
    $('select').click(function(e){      
        var ul = document.createElement('ul');
        $(this).children().each(function(){
            var li = document.createElement('li');
            $(li).html($(this).text());
            $(li).data("value", $(this).val());
            $(ul).append(li);
        });
        $('body').append(ul);
        var cordinates = $(this).offset();
        $(ul).offset({top:cordinates.top + 10 + $(this).height(), left:cordinates.left});
        $(ul).show('slow');
        e.preventDefault();
    }); 
});

The problem here is that when I click on the dropdown the options are shown by default.这里的问题是,当我单击下拉菜单时,默认情况下会显示选项。 I am trying to prevent that.我试图阻止这种情况。 And btw i was doing all this exercise on google chrome browser.顺便说一句,我在 google chrome 浏览器上做所有这些练习。 Later I intend to test other browsers too.后来我也打算测试其他浏览器。 I know that there are libraries that do this, but I just wanted to know the technique used.我知道有一些库可以做到这一点,但我只是想知道使用的技术。

AFAIK, there is no way to do that. AFAIK,没有办法做到这一点。 Your best bet is to use a custom dropdown (plugin or your own implementation).您最好的选择是使用自定义下拉菜单(插件或您自己的实现)。

The ultimate goal is to overlay a div or a ul and display the options where the user can click;最终目标是覆盖一个 div 或一个 ul 并显示用户可以点击的选项; this will then change the option on the select box.这将更改 select 框上的选项。 Is it possible to do this?是否有可能做到这一点?

Btw, from your question, unless you want to change the look of dropdown box, you can stick to the default behavior顺便说一句,从您的问题来看,除非您想更改下拉框的外观,否则您可以坚持默认行为

The procedure you described is a workaround for making HTML forms more beautiful and is almost common.您描述的过程是使 HTML forms 更漂亮并且几乎很常见的解决方法。 For example, a lot of designers create their own checkboxes and hook them with JavaScript to the underlying <input type='checkbox' elements on their form.例如,许多设计人员创建自己的复选框,并将 JavaScript 与表单上的底层<input type='checkbox'元素挂钩。 However, your way of doing it is a little abnormal.但是,你这样做的方式有点不正常。

Do not try to overlay your select element with a div or ul.不要试图用 div 或 ul 覆盖select元素。 Simply create a div, or ul as something that the user sees, and via JavaScript, try to imitate a drop down list.只需创建一个 div 或 ul 作为用户看到的东西,然后通过 JavaScript,尝试模仿一个下拉列表。 Whenever user clicks on any item in your list, get the value and set your select's value accordingly.每当用户单击列表中的任何项目时,获取值并相应地设置您选择的值。

In other words, instead of trying to hook div or ul into select events, match select with click events of div or ul.换句话说,与其尝试将 div 或 ul 挂钩到 select 事件中,不如将 select 与 div 或 ul 的点击事件匹配。

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

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