当键入某个字符时,使用jQuery将光标移动到另一个字段? [英] use jQuery to move the cursor to another field when a certain character is typed?

查看:55
本文介绍了当键入某个字符时,使用jQuery将光标移动到另一个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页上有两个文本字段。用户应输入由连字符( - )字符分隔的两个数字。数字可以是1到10位数。当用户按下连字键时,我需要光标移动到下一个字段。

I have two text fields in my web page. The user is supposed to enter two numbers seperated by a hyphen ("-") character. The numbers may be between 1 and 10 digits each. I need the cursor to move to the next field when the user presses the hyphen key.

我可以使用 $('#轻松移动光标txtField2' )。对焦()。但是,我仍然遇到连字符仍然在第一个文本字段中的问题。如何轻松地抑制连字符出现在第一个文本字段中?

I can easily move the cursor using $('#txtField2').focus(). However, I still have the problem that the hyphen character remains in the first text field. How can I easily supress the hyphen from appearing in the first text field?

推荐答案

HTML



HTML

<form>
    <input type='text' class='num' />
    <input type='text' class='num' />
</form>



JavaScript



JavaScript

$('.num:first').keydown(function (event) {
    // check for hyphen
    if (event.which === 189) {
        event.preventDefault();
        $(this).next('.num').focus();
    }
});

现场演示

这篇关于当键入某个字符时,使用jQuery将光标移动到另一个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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