问题
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