为什么不在SELECT重点提交表单的时候输入? [英] why doesn't hitting enter when a SELECT is focused submit the form?

查看:156
本文介绍了为什么不在SELECT重点提交表单的时候输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下HTML:

 < form action => 
< input />
< select>
< option> A< / option>
< option> B< / option>
< / select>
< input type =submit/>
< / form>

如果焦点位于输入(文本框),然后按Enter键,表单提交。



但是,如果焦点位于选择(我知道我可以找出一些JavaScript来重写这个,但我想知道为什么点击输入不仅仅是工作?



有什么我会通过捕获JavaScript的输入(可能是一些本地键盘下拉的可访问性)来打破?

解决方案

这只是控制的本质。 Enter键(或者一个鼠标点击)是什么使得选择。按Enter键提交表单会导致糟糕的用户体验,因为表单本质上无法使用。 我不推荐通过JavaScript改变行为,只是因为默认行为是规范,每个人都会期望。



(想象一下,当您在下拉列表中进行选择时提交的每个表单都会是什么样子,例如,考虑在Amazon.com上进行搜索,然后一个类别输入搜索词,如果通过按Enter键选择了一个类别,表单会在输入搜索词之前提交。)


Consider the following HTML:

<form action="">
    <input />
    <select>
        <option>A</option>
        <option>B</option>
    </select>
    <input type="submit" />
</form>

If the focus is on the input (text box) and I hit enter, the form submits.

But, if the focus is on the select (dropdown box) and I hit enter, nothing happens.

I know I could figure out some JavaScript to override this, but I want to know why hitting enter doesn't just work?

Is there something I would break by capturing the enter with JavaScript (maybe some native keyboard accessibility of the dropdown)?

解决方案

It's simply the nature of the control. The Enter key (or a mouse click) is what makes the selection. To submit the form when pressing Enter would result in a poor user experience, since the form would essentially be unusable.

I would not recommend changing the behavior via JavaScript, simply because the default behavior is the norm and what everyone will expect.

(Imagine what it would be like if every form submitted when you made a selection in a drop-down list. Consider searching on Amazon.com, for example. One selects a category then enters the search term. If one selected a category by pressing Enter, the form would submit before the search term could be entered.)

这篇关于为什么不在SELECT重点提交表单的时候输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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