当输入文本字段达到最大长度时移动焦点

时间:2009-12-24 19:01:23

标签: javascript jquery javascript-events usability

我有一张信用卡号码表格。这个数字分为四个部分,就像真正的信用卡一样。

我想在表单中添加JavaScript品味,当用户在字段中键入四个字母时,焦点会自动转到下一个标签。但不是在最后一个标签。通过这样做,用户不必键入“tab”键来移动焦点。

可以在标签中添加一些额外的类,ID或名称。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>MoveFocus</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript" charset="utf-8">
     $(function() {
     // some code goes here.
    });
    </script>
</head>


<body>
  <form action="post.php" method="post" accept-charset="utf-8">
    <input type="text" value="" id="first" size="4" maxlength="4"/> -
    <input type="text" value="" id="second" size="4" maxlength="4"/> -
    <input type="text" value="" id="third" size="4" maxlength="4"/> -
    <input type="text" value="" id="fourth" size="4" maxlength="4"/>
    <p><input type="submit" value="Send Credit Card"></p>
  </form>
</body>
</html>

8 个答案:

答案 0 :(得分:28)

我之前没有使用过这个工具,但它可以满足您的需求。你可以看一下它的来源以获得一些想法:

This Plugin on GitHub

根据您的情况,您可以添加以下代码:

<script type="text/javascript" src="jquery.autotab.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('#first').autotab({ target: '#second', format: 'numeric' });
    $('#second').autotab({ target: '#third', format: 'numeric', previous: '#first' });
    $('#third').autotab({ previous: '#second', format: 'numeric' });
});
</script>

答案 1 :(得分:15)

正如其他人所敦促的那样,不要这样做。用户无法预料到您会自动标记它们,这将使他们疯狂。您是否考虑过复制并粘贴信用卡的用户?无论如何使用四个字段有什么好处?

此外,并非所有信用卡都将其数字分为四组四组。例如,美国运通将它们分为三组数字。在这种情况下,动态添加和删除文本字段会遇到麻烦。

相反,使用您的Javascript自动插入空格它们所属的位置,推进光标,而不是焦点。数字中的第一个数字表示信用卡的类型(5是万事达卡,4是Visa,3是美国运通......),因此您可以阅读此信息以决定在哪里添加空格。发布时,从字符串中擦除空格。这种方法可以为您和您的用户带来很多痛苦。

答案 2 :(得分:9)

正如@Sander建议的那样,执行自动选项卡的简便方法是:

jQuery("form input[type=text]").on('input',function () {
    if(jQuery(this).val().length == jQuery(this).attr('maxlength')) {
        jQuery(this).next("input").focus();
    }
});
@ morespace54

更新

oninput IE9 + 支持 html5 事件,因此您可以使用keyup代替。

答案 3 :(得分:6)

我强烈建议使用Masked Input jQuery插件。 http://digitalbush.com/projects/masked-input-plugin/

您的用法如下:

$('#creditCardNumber').mask("9999-9999-9999-9999");

这样您就可以获得复制粘贴支持。

答案 4 :(得分:3)

如果您的表单字段与示例中的字段不同,则可以简单地利用nextElementSibling和voilà!

function skipIfMax(element) {
  max = parseInt(element.dataset.max)
  
  
  if (element.value.length >= max && element.nextElementSibling) {
    element.nextElementSibling.focus();  
  }
}
<input type="text" data-max=2 oninput="skipIfMax(this)"/>
<input type="text" data-max=3 oninput="skipIfMax(this)"/>
<input type="text" data-max=4 oninput="skipIfMax(this)"/>
<input type="text" data-max=5 oninput="skipIfMax(this)"/>

答案 5 :(得分:1)

一个非常简单的解决方案可能是这样的:

<script type="text/javascript">
    function input_onchange(me){ 
        if (me.value.length != me.maxlength){
            return;
        }
        var i;
        var elements = me.form.elements;
        for (i=0, numElements=elements.length; i<numElements; i++) {
            if (elements[i]==me){
                break;
            }
        }
        elements[i+1].focus();
    }
</script>
<form action="post.php" method="post" accept-charset="utf-8">
    <input type="text" value="" id="first" size="4" maxlength="4"
        onchange="input_onchange(this)"
    /> -
    <input type="text" value="" id="second" size="4" maxlength="4"
        onchange="input_onchange(this)"
    /> -
    <input type="text" value="" id="third" size="4" maxlength="4"
        onchange="input_onchange(this)"
    /> -
    <input type="text" value="" id="fourth" size="4" maxlength="4"
        onchange="input_onchange(this)"
    /> -
    <p><input type="submit" value="Send Credit Card"></p>
</form>

答案 6 :(得分:0)

我没有测试过,但我认为这样可行。当第4个字段完成时,它可能还会将焦点移动到按钮上。

$("form input").change(function () {
    var maxLength = $(this).attr('maxlength');

    if($(this).val().length == maxLength) {
        $(this).next().focus();
    }
}

答案 7 :(得分:0)

这没有四个字段,但它确实验证了信用卡(完整性检查,而不是Luhn的算法!)。我必须告诉你对用户和自动标签使用多个字段有多烦人。我建议你只使用一个字段。

来自jquery website:

$("#myform").validate({
  rules: {
    field: {
      required: true,
      creditcard: true
    }
  }
});

/

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/lib/jquery.delegate.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
<script type="text/javascript">
jQuery.validator.setDefaults({
    debug: true,
    success: "valid"
});;
</script>

  <script>
  $(document).ready(function(){
    $("#myform").validate({
  rules: {
    field: {
      required: true,
      creditcard: true
    }
  }
});
  });
  </script>
  <style>#field { margin-left: .5em; float: left; }
    #field, label { float: left; font-family: Arial, Helvetica, sans-serif; font-size: small; }
    br { clear: both; }
    input { border: 1px solid black; margin-bottom: .5em;  }
    input.error { border: 1px solid red; }
    label.error {
        background: url('http://dev.jquery.com/view/trunk/plugins/validate/demo/images/unchecked.gif') no-repeat;
        padding-left: 16px;
        margin-left: .3em;
    }
    label.valid {
        background: url('http://dev.jquery.com/view/trunk/plugins/validate/demo/images/checked.gif') no-repeat;
        display: block;
        width: 16px;
        height: 16px;
    }
</style>
</head>
<body>

<form id="myform">
  <label for="field">Required, creditcard (try 446-667-651): </label>
  <input class="left" id="field" name="field" />
  <br/>
  <input type="submit" value="Validate!" />
</form>

</body>
</html>
相关问题