如何使用 javascript 跳过字段?

发布于 2024-07-04 19:51:15 字数 998 浏览 6 评论 0原文

我有一个这样的表单:

<form name="mine">
    <input type=text name=one>
    <input type=text name=two>
    <input type=text name=three>
</form>

当用户在“一”中输入值时,我有时想跳过字段“二”,具体取决于他输入的内容。 例如,如果用户键入“123”并使用 Tab 移至下一个字段,我想跳过它并转到字段三。

我尝试使用 OnBlurOnEnter,但没有成功。

尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

杀手六號 2024-07-11 19:51:15

如果您使用了您所描述的方法,并且它们有效,那么当用户单击该字段时,焦点也会发生变化,而不是通过 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.

云雾 2024-07-11 19:51:15
<form name="mine">
    <input type="text" name="one" onkeypress="if (mine.one.value == '123') mine.three.focus();" />
    <input type="text" name="two">
    <input type="text" name="three">
</form>
<form name="mine">
    <input type="text" name="one" onkeypress="if (mine.one.value == '123') mine.three.focus();" />
    <input type="text" name="two">
    <input type="text" name="three">
</form>
浅语花开 2024-07-11 19:51:15

尝试使用 onkeypress 而不是 onblur。 另外,在第二个字段的 onfocus 上,您应该将其发送到第三个字段。 我假设您不希望他们在 1 为 123 时输入 2,因此您只需在 2 的 onfocus 上检查该值,然后发送到 3。

Try onkeypress instead of onblur. Also, on the onfocus 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's onfocus and send on to three.

闻呓 2024-07-11 19:51:15

尝试将 tabindex 属性附加到您的元素,然后以编程方式(在 javaScript 中更改它):

<INPUT tabindex="3" type="submit" name="mySubmit">

Try to attach tabindex attribute to your elements and then programmaticaly (in javaScript change it):

<INPUT tabindex="3" type="submit" name="mySubmit">
柳若烟 2024-07-11 19:51:15

您可以在第二个字段上使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文