如何使用 javascript 跳过字段?
我有一个这样的表单:
<form name="mine">
<input type=text name=one>
<input type=text name=two>
<input type=text name=three>
</form>
当用户在“一”中输入值时,我有时想跳过字段“二”,具体取决于他输入的内容。 例如,如果用户键入“123”并使用 Tab 移至下一个字段,我想跳过它并转到字段三。
我尝试使用 OnBlur
和 OnEnter
,但没有成功。
尝试 1:
<form name="mine">
<input type=text name=one onBlur="if (document.mine.one.value='123') document.three.focus();>
<input type=text name=two>
<input type=text name=three>
</form>
尝试 2:
<form name="mine">
<input type=text name=one>
<input type=text name=two onEnter="if (document.mine.one.value='123') document.three.focus();>
<input type=text name=three>
</form>
但这些都不起作用。 看起来浏览器不允许您在焦点改变时扰乱焦点。
顺便说一句,所有这些都在 Linux 上的 Firefox 上进行了尝试。
I have a form like this:
<form name="mine">
<input type=text name=one>
<input type=text name=two>
<input type=text name=three>
</form>
When user types a value in 'one', I sometimes want to skip the field 'two', depending on what he typed. For example, if user types '123' and uses Tab to move to next field, I want to skip it and go to field three.
I tried to use OnBlur
and OnEnter
, without success.
Try 1:
<form name="mine">
<input type=text name=one onBlur="if (document.mine.one.value='123') document.three.focus();>
<input type=text name=two>
<input type=text name=three>
</form>
Try 2:
<form name="mine">
<input type=text name=one>
<input type=text name=two onEnter="if (document.mine.one.value='123') document.three.focus();>
<input type=text name=three>
</form>
but none of these works. Looks like the browser doesn't allow you to mess with focus while the focus is changing.
BTW, all this tried with Firefox on Linux.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用了您所描述的方法,并且它们有效,那么当用户单击该字段时,焦点也会发生变化,而不是通过 Tab 键切换到该字段。 我可以向你保证,这会让用户感到沮丧。 (我不知道为什么它不起作用。)
相反,如前所述,一旦字段一的内容发生更改,就更改相应字段的 tabindex。
If you used the method you describe, and they worked, the focus would also change when the user clicks on the field, instead of tabbing to it. I can guarantee you that this would result in a frustrated user. (Why exactly it doesn't work is beyond me.)
Instead, as said before, change the tabindex of the appropriate fields as soon as the content of field one changes.
尝试使用
onkeypress
而不是onblur
。 另外,在第二个字段的onfocus
上,您应该将其发送到第三个字段。 我假设您不希望他们在 1 为 123 时输入 2,因此您只需在 2 的onfocus
上检查该值,然后发送到 3。Try
onkeypress
instead ofonblur
. Also, on theonfocus
of field two is where you should be sending to three. I'm assuming you don't want them typing in two if one is 123 so you can just check that on two'sonfocus
and send on to three.尝试将 tabindex 属性附加到您的元素,然后以编程方式(在 javaScript 中更改它):
Try to attach tabindex attribute to your elements and then programmaticaly (in javaScript change it):
您可以在第二个字段上使用 onfocus 事件,该事件在收到焦点时将被调用。 此时,字段 1 的值应更新,然后您可以执行检查。
You could use the onfocus event on field two, which will be called when it receives focus. At that point, field 1's value should be updated and you can perform your check then.