如何使用javascript跳过字段?

时间:2022-09-10 11:49:00

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.

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

I tried to use OnBlur and OnEnter, without success.

我尝试使用OnBlur和OnEnter,但没有成功。

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.

顺便说一下,这一切都是在Linux上用Firefox试过的。

5 个解决方案

#1


3  

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

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

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

#2


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.

您可以在字段2上使用onfocus事件,它将在接收焦点时调用。此时,应更新字段1的值,然后您可以执行检查。

#3


1  

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.

相反,如前所述,只要字段1的内容发生变化,就更改相应字段的tabindex。

#4


0  

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

#5


0  

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.

试试onkeypress而不是onblur。另外,在第二场的焦点上你应该发送到三个。我假设你不希望他们输入两个,如果一个是123,所以你可以检查两个onfocus并发送到三个。

#1


3  

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

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

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

#2


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.

您可以在字段2上使用onfocus事件,它将在接收焦点时调用。此时,应更新字段1的值,然后您可以执行检查。

#3


1  

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.

相反,如前所述,只要字段1的内容发生变化,就更改相应字段的tabindex。

#4


0  

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

#5


0  

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.

试试onkeypress而不是onblur。另外,在第二场的焦点上你应该发送到三个。我假设你不希望他们输入两个,如果一个是123,所以你可以检查两个onfocus并发送到三个。