【问题标题】:Laravel and Jquery Var LengthLaravel 和 Jquery 可变长度
【发布时间】:2016-06-26 15:17:16
【问题描述】:

我需要为我的输入创建一个验证以将其限制为 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

【问题讨论】:

标签: javascript jquery laravel laravel-4


【解决方案1】:

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

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

如上所述,您的 JQuery 验证代码中可能没有引用正确的 html 元素。所以将 $('#send-txt') 更改为 $('#code') (不要忘记添加 ID 属性根据您的意见)

【讨论】:

    猜你喜欢
    • 2010-10-10
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2020-11-20
    • 2013-08-01
    • 2017-02-06
    相关资源
    最近更新 更多