最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - reset button in form doesn't reset selection to first element because of selected keyword in HTML - Stack O

programmeradmin6浏览0评论

I have a reset button in a form

<input type="reset" value="Reset">

and some selections

<select>
<option value="0">A</option>
<option value="10" selected="selected">B</option>
</select>

If I click on reset nothing happens. Here is an example.

Edit: I thought that the reset button will reset to the first value of a selection list. It seems that was a missunderstanding of my side. I want to reset to the first element of a selection.

So is the only way to create a reset function in Javascript? What if the user has turned off Javascript?

I have a reset button in a form

<input type="reset" value="Reset">

and some selections

<select>
<option value="0">A</option>
<option value="10" selected="selected">B</option>
</select>

If I click on reset nothing happens. Here is an example.

Edit: I thought that the reset button will reset to the first value of a selection list. It seems that was a missunderstanding of my side. I want to reset to the first element of a selection.

So is the only way to create a reset function in Javascript? What if the user has turned off Javascript?

Share Improve this question edited Jul 29, 2012 at 10:21 Mat 207k41 gold badges402 silver badges418 bronze badges asked Apr 6, 2012 at 8:30 testingtesting 20.3k54 gold badges243 silver badges432 bronze badges 7
  • Then you are SOL. -You COULD make the reset a submit button that requested a new form with the selected on item 0 – mplungjan Commented Apr 6, 2012 at 8:31
  • @testing, your fiddle works for me on Firefox 10 (selecting another option then clicking Reset restores product a as the selected value). What browser are you using? – Frédéric Hamidi Commented Apr 6, 2012 at 8:33
  • What is SOL? I'm using Firefox 11, Chrome 17, IE 9 ... I've bad experiences with multiple submit buttons, because this.form.submit(); doesn't work anymore. Product a is selected per default. When the user clicks on Reset it should show "-none-". – testing Commented Apr 6, 2012 at 8:53
  • 2 @testing, this is not the way reset buttons work. The value that was initially selected when the page loaded will be restored, not the first value of the list. – Frédéric Hamidi Commented Apr 6, 2012 at 8:55
  • So how can I reset to the first value of the list? – testing Commented Apr 6, 2012 at 8:58
 |  Show 2 more ments

2 Answers 2

Reset to default 3

Force value to nothing of all inputs, select, etc...

$("#filter_form").find("select, input, checkbox").val("");

When I click on reset button, form reset in defaults. It's working in your example (option 10 is default).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论