jQuery:一次防止多个输入

时间:2016-10-28 09:29:48

标签: javascript jquery html forms input

我正在编写一个jquery代码,它应该使用我在变量中定义的字符来切换输入字符。到目前为止,如果您一次只按一个字符,代码就可以很好地工作。但是一旦你一次击中多个字符,你就会开始打破结果。有什么样的选项可以让jquery一次只获取一个char或正确读出并转换多个chars。

多个字符按下太快或类似按下的结果导致变量的字符串混乱或/和混合的多个输入中有一些字符。

我已经了解了.prop()函数来禁用输入,但即使我禁用了输入,也无法阻止多个字符传递给函数。

提前谢谢你们, 欢呼声

HTML:



    jQuery(document).ready(function(){
        String.prototype.removeChar = function(){
            var string = "";
            for(var i = 0, length = this.length-1; i<length; i++){
                string += this.charAt(i);
            }
            return string;
        }
        var name = "Some Random Name",
            length,
            sub = "",
            event,
            array=[];
        $("form").on("keyup", function(e){
            event = e.keyCode;
            if (e.keyCode === 8){
                length = $(".input").val().length;
                sub = sub.removeChar();
                $(".input").val(sub);
            }else{
                length = $(".input").val().length;
                sub += name.charAt(length-1);
                $(".input").val(sub);
                }
        });
        
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>Who should be President?<input type="text" name="president" class="input"></form>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

改变你的

$(".input").val(sub);

$(".input").val(name.substring(0, length));

然后将您的条件更改为

if (e.keyCode !== 8){
    length = $(".input").val().length;
    sub += name.charAt(length-1);
    $(".input").val(name.substring(0, length));
}

在这种情况下,如果单击后退按钮

,则不执行任何操作

    jQuery(document).ready(function(){
        String.prototype.removeChar = function(){
            var string = "";
            for(var i = 0, length = this.length-1; i<length; i++){
                string += this.charAt(i);
            }
            return string;
        }
        var name = "Some Random Name",
            length,
            sub = "",
            event,
            array=[];
        $("form").on("keyup", function(e){
            event = e.keyCode;
            if (e.keyCode !== 8){
                length = $(".input").val().length;
                sub += name.charAt(length-1);
                $(".input").val(name.substring(0, length));
            }
        });
        
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>Who should be President?<input type="text" name="president" class="input"></form>

答案 1 :(得分:1)

只需用随机名称替换整个输入值并剪切val short

 $("form").on("keyup", function(e){
            event = e.keyCode;
            if (e.keyCode != 8){

                length = $(".input").val().length;
                //sub += name.charAt(length-1);
                $(".input").val(name.substring(0, length));

                }
        });

答案 2 :(得分:1)

只需更改

  sub += name.charAt(length-1);

sub = name.substring(0, length);

想法是获取用户输入的字符总数,并使用substring函数从name变量中获取多个字符

jQuery(document).ready(function(){
        String.prototype.removeChar = function(){
            var string = "";
            for(var i = 0, length = this.length-1; i<length; i++){
                string += this.charAt(i);
            }
            return string;
        }
        var name = "Some Random Name",
            length,
            sub = "",
            event,
            array=[];
        $("form").on("keyup", function(e){
            event = e.keyCode;
            if (e.keyCode === 8){
                length = $(".input").val().length;
                sub = sub.removeChar();
                $(".input").val(sub);
            }else{
                length = $(".input").val().length;
                sub = name.substring(0, length);  // this is the change
                $(".input").val(sub);
                }
        });
        
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>Who should be President?<input type="text" name="president" class="input"></form>