使用 JavaScript 或 jQuery 设置文本框的最大长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6141680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting maxlength of textbox with JavaScript or jQuery
提问by shebelaw
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 更改文本框的 maxlength:我尝试了以下操作,但似乎没有帮助:
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?
我究竟做错了什么?
回答by Jay
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');
});
回答by Eli
The max length property is camel-cased: maxLength
max length 属性是驼峰式的: maxLength
jQuery doesn't come with a maxlength method by default. Also, your document ready function isn't technically correct:
默认情况下,jQuery 没有提供 maxlength 方法。此外,您的文档就绪功能在技术上不正确:
$(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);
});
回答by hunter
set the attribute, not a property
设置属性,而不是属性
$("#ms_num").attr("maxlength", 6);
回答by Monzur
without jQuery you can use
没有 jQuery 你可以使用
document.getElementById('text_input').setAttribute('maxlength',200);
回答by Knox
$('#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 和选择器缓存一起使用以提高性能,并且成功了。
回答by Div Sol
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;
}
});
回答by Evan TOder
<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>
回答by user7212232
<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>