允许用户从html< select>中选择null.落下 [英] Allowing a user to select null from an html <select> drop down

查看:91
本文介绍了允许用户从html< select>中选择null.落下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个下拉选择框.该字段不是必需的.如果用户不小心进行了选择,是否有一种简单的方法可以让用户将字段重置为空?

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?

我唯一能想到的就是让第一个选项是请进行选择...",然后查看用户是否选择了该选项,并将相应的值设置为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.

推荐答案

此示例将使用户能够重置选择,而不会看到空"选项. http://jsbin.com/ojozar/2

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();
  });

这篇关于允许用户从html&lt; select&gt;中选择null.落下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆