Laravel和Jquery Var长度

时间:2016-03-11 11:31:21

标签: javascript jquery laravel laravel-4

我需要为我的输入创建一个验证来限制它10个字符, 我不知道为什么我的代码不起作用

{{ Form::label('send-txt', 'Category Code', array('class' => 'control-label')) }}
{{ Form::text('code', null, array('class' => 'form-control', 'placeholder' => 'Category Code')) }}


  $(document).ready( function() {        
        var maxLen = 10;

        $('#send-txt').keypress(function(event){
            var Length = $("#send-txt").val().length;
            var AmountLeft = maxLen - Length;
            $('#txt-length-left').html(AmountLeft);
            if(Length >= maxLen){
                if (event.which != 8) {
                    return false;
                }
            }
        });

});

我使用了laravel 4.2

1 个答案:

答案 0 :(得分:1)

您也可以使用HTML5属性对其进行验证。

{{ Form::text('code', null, array('class' => 'form-control', 'placeholder' => 'Category Code', 'maxlength' => 10 )) }}

如上所述,也许您没有在JQuery验证代码中引用正确的html元素。所以将 $('#send-txt')更改为 $('#code')(不要忘记将 ID 属性添加到您的输入中