HTML中怎样判断出input标签输入框输入的实际字符的长度

html中并无法直接获取input的内容的长度.

一般推荐您使用JS来配合计算


<input type="text" id="username" onpropertychange="getlen()">
<input type="text" id="lennum" value="0">
<script>
function getlen(){
 document.getElementById('lennum').value=document.getElementById('username').value.length;
}
</script>

效果:、

2个回答

#合辑# 机票是越早买越便宜吗?

wanyanjiabin
2017-06-04 · 知道合伙人软件行家

关注

html中并无法直接获取input的内容的长度.

一般推荐您使用JS来配合计算

1

2

3

4

5

6

7

<input type="text" id="username" onpropertychange="getlen()">

<input type="text" id="lennum" value="0">

<script>

function getlen(){

 document.getElementById('lennum').value=document.getElementById('username').value.length;

}

</script>

效果:

这里注意使用的onpropertychange事件仅仅支持IE浏览器,您也可以使用HTML5自带的事件oninput.但是oninput仅支持支持HTML5的浏览器,IE9以下无效.

猜你喜欢

转载自blog.csdn.net/tianxianghuiwei/article/details/134093806