检查字段长度作为用户类型

Dan*_*els 4 javascript validation jquery live

嘿伙计们,我已经在网上看到了这种能力,我不太确定如何搜索它,一个很好的例子就是发送一条短信,它会在你输入时说:

132/160 - 132计数器将增加,直到达到极限.

我的问题是,是否有可能单独使用javascript,没有jQuery库?但是,如果我必须使用jQuery,我可能会指向一个好的教程,或者它可能比这简单,甚至一些术语来搜索它,谢谢.

And*_*y E 6

使用oninput支持的事件(所有现代浏览器,IE 9及更高版本)和onpropertychange旧版Internet Explorer:

var myInput = document.getElementById("myInput");
if ("onpropertychange" in myInput && !("oninput" in myInput)) {
    myInput.onpropertychange = function () { 
        if (event.propertyName == "value")
            inputChanged.call(this, event);
    }
}
else
    myInput.oninput = inputChanged;

function inputChanged () {
    // Check the length here, e.g. this.value.length
}
Run Code Online (Sandbox Code Playgroud)

onkeyup不适合处理输入,因为输入文本的用户和注意到更改的代码之间存在明显的延迟.用户甚至可以按住键(用于在Windows设备上重复文本),但是您的代码无法处理它.


Dut*_*432 2

<!--This is your input box. onkeyup, call checkLen(...) -->
<input type="text" id="myText" maxlength="200" onkeyup="checkLen(this.value)">

<!--This is where the counter appears -->
<div id="counterDisplay">0 of 200</div>


<!--Javascript Code to count text length, and update the counter-->
<script type="text/javascript"><!--
    function checkLen(val){
        document.getElementById('counterDisplay').innerHTML = val.length + ' of 200';
    }
//--></script>
Run Code Online (Sandbox Code Playgroud)