简体   繁体   English

使用 JavaScript 或 jQuery 设置文本框的最大长度

[英]Setting maxlength of textbox with JavaScript or jQuery

I want to change the maxlength of a textbox with JavaScript or jQuery: I tried the following but it didn't seem to help:我想用 JavaScript 或 jQuery 更改文本框的最大长度:我尝试了以下方法,但似乎没有帮助:

var a = document.getElementsByTagName('input');
for(var i=0; i<a.length; i++) {
    if((a[i].type!= 'radio')||(a[i].type!= 'checkbox'))
        a[i].maxlength = 5;
}
document.getElementsByTagName('input')[1].maxlength="3";

$().ready(function()
{
    $("#inputID").maxlength(6);
});

What am I doing wrong?我究竟做错了什么?

Not sure what you are trying to accomplish on your first few lines but you can try this:不确定您在前几行中要完成什么,但您可以尝试以下操作:

$(document).ready(function()
{
    $("#ms_num").attr('maxlength','6');
});

The max length property is camel-cased: maxLength最大长度属性是驼峰式的: maxLength

jQuery doesn't come with a maxlength method by default. jQuery 默认不提供 maxlength 方法。 Also, your document ready function isn't technically correct:此外,您准备好的文档 function 在技术上不正确:

$(document).ready(function () {
    $("#ms_num")[0].maxLength = 6;
    // OR:
    $("#ms_num").attr('maxlength', 6);
    // OR you can use prop if you are using jQuery 1.6+:
    $("#ms_num").prop('maxLength', 6);
});

Also, since you are using jQuery, you can rewrite your code like this (taking advantage of jQuery 1.6+):此外,由于您使用的是 jQuery,您可以像这样重写您的代码(利用 jQuery 1.6+):

$('input').each(function (index) {
    var element = $(this);
    if (index === 1) {
        element.prop('maxLength', 3);
    } else if (element.is(':radio') || element.is(':checkbox')) {
        element.prop('maxLength', 5);
    }
});

$(function() {
    $("#ms_num").prop('maxLength', 6);
});

without jQuery you can use没有 jQuery 你可以使用

document.getElementById('text_input').setAttribute('maxlength',200);

set the attribute, not a property设置属性,而不是属性

$("#ms_num").attr("maxlength", 6);
$('#yourTextBoxId').live('change keyup paste', function(){
    if ($('#yourTextBoxId').val().length > 11) {
        $('#yourTextBoxId').val($('#yourTextBoxId').val().substr(0,10));
    }
});

I Used this along with vars and selectors caching for performance and that did the trick..我将它与 vars 和选择器缓存一起使用以提高性能,并且成功了..

You can make it like this:你可以这样:

$('#inputID').keypress(function () {
    var maxLength = $(this).val().length;
    if (maxLength >= 5) {
        alert('You cannot enter more than ' + maxLength + ' chars');
        return false;
    }
});
<head>
    <script type="text/javascript">
        function SetMaxLength () {
            var input = document.getElementById ("myInput");
            input.maxLength = 10;
        }
    </script>
</head>
<body>
    <input id="myInput" type="text" size="20" />
</body>
<head>
    <script type="text/javascript">
        function SetMaxLength () {
            var input = document.getElementById("myInput");
            input.maxLength = 10;
        }
    </script>
</head>
<body>
    <input id="myInput" type="text" size="20" />
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM