javascript - Select dropdown list not closing after selection - Stack Overflow

My issue is when I click on any item from the list the dropdown is not populated automatically and the

My issue is when I click on any item from the list the dropdown is not populated automatically and the list is not closed.

HTML code for dropdown list:

<label id='choose' for='options'>Select options</label>
<br clear='all' />
<select id='options' size="10" style='display:none;'>
    <option>dasd</option>
    <option>asdaaadsdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
</select>

Fiddle link.

My issue is when I click on any item from the list the dropdown is not populated automatically and the list is not closed.

HTML code for dropdown list:

<label id='choose' for='options'>Select options</label>
<br clear='all' />
<select id='options' size="10" style='display:none;'>
    <option>dasd</option>
    <option>asdaaadsdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
</select>

Fiddle link.

Share edited Oct 27, 2015 at 9:39 Anders 8,65310 gold badges60 silver badges99 bronze badges asked Oct 27, 2015 at 9:23 ZivaZiva 2331 gold badge4 silver badges19 bronze badges 3
  • Can you please explain a little better what is your problem? – IlGala Commented Oct 27, 2015 at 9:27
  • Ideally when we click on option from the dropdown, the drop down closes with the option we select. But it my case the drop down remains open even when I select the option. Check fiddle for reference – Ziva Commented Oct 27, 2015 at 9:29
  • Why are you working on a label? set the first option with value -1 as "Select options"... – IlGala Commented Oct 27, 2015 at 9:38
Add a ment  | 

3 Answers 3

Reset to default 1

Add this to your script

$("#options option").click(function(){
    $("#choose").text($(this).text());
    $(this).parent().hide();
});

It looks like you are trying to recreate an already existing functionality by using JS and CSS, when some good old fashion HTML would do it for you. Remove the extra script and styling, and simply use:

<label id='choose' for='options'>Select options</label>
<br clear='all' />
<select id='options'>
    <option>dasd</option>
    <option>asdaaadsdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
    <option>asdasdasd</option>
</select>

Please note that I removed the size="10" and style='display:none;' parts. The browser will take care of things like opening and closing the list of options, the down arrow, etc.

<select id='options'>
    <option value="0">--Select Options--</option>
    <option>abc</option>
    <option>bcd</option>
    <option>cde</option>
    <option>def</option>
    <option>efg</option>
    <option>fgh</option>
    <option>ghi</option>
    <option>hij</option>
    <option>ijk</option>
    <option>jkl</option>
</select>

I think this can help you

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744949733a4602845.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信