Allowing a user to select null from an html <select> drop down

北城以北 提交于 2019-12-08 12:59:40

问题


I've got a dropdown select box on my site. The field is not required. If the user accidentally makes a selection, is there an easy way to allow the user to reset the field to null?

The only thing I can think is to have the first option be a "Please make a selection..." and then see if the user has selected that option, and set the corresponding value to null. That just feels kinda hacky.


回答1:


this example will make the user able to reset the select without seeing the "null" option. http://jsbin.com/ojozar/2

  <select id="select">
    <option value="1" >option 1</option>
    <option value="2" >option 2</option>
    <option value="3" >option 3</option>
  </select>
  <input type="button" value="reset" id="resetbtn" />  

js :

  $('#resetbtn').click(function(){
    $('select#select').prepend('<option selected="selected" value="null" class="null"></option>');        
  });

  $('select#select').bind('focus click mouseup',function(){
    $(this).find('option.null').remove();
  });


来源:https://stackoverflow.com/questions/8231555/allowing-a-user-to-select-null-from-an-html-select-drop-down

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