使用javascript设置maxlength

使用javascript设置maxlength,javascript,html,forms,Javascript,Html,Forms,我正在尝试使用JavaScript动态设置输入字段的maxlength。显然这是IE中的一个问题,我找到了部分解决方案 $("input#title").get(0).setAttribute("max_length", 25); $("input#title").get(0).setAttribute( "onkeypress", "return limitMe(event, this)");

我正在尝试使用JavaScript动态设置输入字段的maxlength。显然这是IE中的一个问题,我找到了部分解决方案

$("input#title").get(0).setAttribute("max_length", 25);
$("input#title").get(0).setAttribute(
                        "onkeypress", 
                        "return limitMe(event, this)");

function limitMe(evt, txt) {
    if (evt.which && evt.which == 8) return true;
    else return (txt.value.length < txt.getAttribute("max_length");
}
$(“输入标题”).get(0).setAttribute(“最大长度”,25);
$(“输入标题”).get(0).setAttribute(
“onkeypress”,
“返回限制(事件,本)”;
函数limitMe(evt,txt){
if(evt.which&&evt.which==8)返回true;
else返回(txt.value.length
它可以在Firefox中使用,但由于某些原因不能在IE中使用。但是,它可以在如下设置的输入字段中使用:

<input type="text" max_length="25" onkeypress="return limitMe(event, this);"/>


但是由于输入字段是动态创建的,我不能这样做……有什么想法吗?

您要查找的属性是
maxlength
,而不是
max\u length
。请参阅。

如果您使用的是jQuery,那么为什么不充分利用它的抽象呢

例如

而不是:

$("input#title").get(0).setAttribute("max_length", 25);
$("input#title").get(0).setAttribute(
                        "onkeypress", 
                        "return limitMe(event, this)");
function limitMe(evt, txt) {
    if (evt.which && evt.which == 8) return true;
    else return (txt.value.length < txt.getAttribute("max_length");
}
$(“输入标题”).get(0).setAttribute(“最大长度”,25);
$(“输入标题”).get(0).setAttribute(
“onkeypress”,
“返回限制(事件,本)”;
函数limitMe(evt,txt){
if(evt.which&&evt.which==8)返回true;
else返回(txt.value.length
这样做:

$('input#title').attr('maxLength','25').keypress(limitMe);

function limitMe(e) {
    if (e.keyCode == 8) { return true; }
    return this.value.length < $(this).attr("maxLength");
}
$('input#title').attr('maxLength','25').keypress(limitMe);
函数极限(e){
如果(e.keyCode==8){返回true;}
返回this.value.length<$(this.attr(“maxLength”);
}


编辑:它在IE中不起作用的原因可能是因为您通过setAttribute附加了“onKeyPress”处理程序。-这不是注册事件处理程序的正确方法。

但不要忘记,如果您通过JavaScript设置maxlength,其他人也可以轻松地将maxlength更改为他们喜欢的任何值。您将仍然需要在服务器上验证提交的数据。

因为我对@James的回答有一些问题,所以我写了一些东西,即使在复制粘贴内容时也能工作,尤其是在使用IE8和IE8以下版本时。我的实现使用jQuery及其实时事件

jQuery(function () {
    $('body').on('keydown.maxlength_fix', 'textarea[maxlength]', function (e) {
        var $this = $(this);
        window.setTimeout(function () {
            var val = $this.val();
            var maxlength = $this.attr('maxlength');
            if (val.length > maxlength) {
                $this.val(val.substr(0, maxlength));
            }
        }, 4);
    });
});

我的代码还打印出writed/max字符,忽略该部分

$("[maxlength]").bind("keyup focusin", function(){
    var maxkey = $(this).attr("maxlength");
    var length = $(this).val().length;
    var value = $(this).val();
    $(this).parent().find(".counter").text(length+"/"+maxkey);
    if (length > maxkey) $(this).val(value.substring(0, maxkey));
}).bind("focusout", function(){$(this).parent().find(".counter").text("")});

IE在setAttribute()上区分大小写,只有“maxLength”有效


这里的所有答案都依赖于按键/粘贴事件。下面是我使用
输入
事件的解决方案:

$('input').on('input', onInput);

var inputValue = '';

function onInput(e) {
    if(e.currentTarget.value.length > 30) {
        e.currentTarget.value = titleValue;
        return;
    }

    inputValue = e.currentTarget.value;
}

很抱歉,我应该包括一个事实,即您不能在IE中动态设置“maxlength”。我发现jQuery的attr()实际上能够以正确的方式设置maxlength,所以我不必使用自己的函数。太好了!谢谢,伙计(:
$('input').on('input', onInput);

var inputValue = '';

function onInput(e) {
    if(e.currentTarget.value.length > 30) {
        e.currentTarget.value = titleValue;
        return;
    }

    inputValue = e.currentTarget.value;
}