setting maxlength using javascript

后端 未结 7 1009
终归单人心
终归单人心 2021-01-05 05:14

I\'m trying to set the maxlength on input fields dynamically using JavaScript. Apparently that is a problem in IE, and I found part of the solution.

$(\"inpu         


        
7条回答
  •  礼貌的吻别
    2021-01-05 06:09

    All the answers here relies on keypress/paste events. Here is my solution using the input event:

    $('input').on('input', onInput);
    
    var inputValue = '';
    
    function onInput(e) {
        if(e.currentTarget.value.length > 30) {
            e.currentTarget.value = titleValue;
            return;
        }
    
        inputValue = e.currentTarget.value;
    }
    

提交回复
热议问题