如何使用javascript跳过字段?

时间:2020-03-05 18:57:44  来源:igfitidea点击:

我有这样的形式:

<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上进行了尝试。

解决方案

回答

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

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

回答

我们可以在第二个字段上使用onfocus事件,该事件在获得焦点时将被调用。此时,字段1的值应该被更新,然后我们可以执行检查。

回答

如果我们使用了我们描述的方法,并且它们起作用了,那么当用户单击该字段时,焦点也会发生变化,而不是跳到该字段上。我可以向我们保证,这会使用户感到沮丧。 (为什么它不起作用超出了我。)

相反,如前所述,一旦字段一的内容更改,就更改相应字段的tabindex。

回答

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

回答

尝试使用" onkeypress"而不是" onblur"。另外,在字段2的" onfocus"上,我们应该将其发送给三个字段。我假设我们不希望他们输入两个(如果一个是123),那么我们只需检查两个的onfocus并发送给三个即可。