HTML textarea失去焦点事件中,如果内容超过了50个字符,只留下前面50个字符 js怎

HTML textarea失去焦点事件中,如果内容超过了50个字符,只留下前面50个字符 js怎,第1张

一般 做法 就是强制现在50个字符啊,超过就不能输入了

仅供参考

function strLenCalc(obj, checklen, maxlen) {

var v = obj.val(), charlen = 0, maxlen = !maxlen ? 2000 : maxlen, curlen = maxlen, len = v.length

if(v !='')

$('#sub').trigger('click')

for(var i = 0i <v.lengthi++) {

if(v.charCodeAt(i) <0 || v.charCodeAt(i) >255) {

curlen -= 1

}

}

if(curlen >= len) {

$("#"+checklen).html("还可输入 <strong class=\"lans\">"+Math.floor((curlen-len)/2)+"</strong>个字").css('color', '')

$("#subBtn").removeAttr("disabled")

} else {

$("#"+checklen).html("已经超过 <strong class=\"lans\">"+Math.ceil((len-curlen)/2)+"</strong>个字").css('color', '#FF0000')

$("#subBtn").attr("disabled", "disabled")

}

}

简单改了下你的,效果实现了,规范的写法自己改改吧

<!DOCTYPE html>  

<html>  

<head>  

    <meta charset="UTF-8"/>  

    <title>checkValidity 示例</title> 

    <style>

      input.dd::-webkit-input-placeholder{

          color: red

          opacity:1

      } 

       

    </style>

</head>  

<body> 

<form action="" method="get">

<table width="200%" border="0" cellspacing="0" cellpadding="0" >

  <tr>

    <td><input class='' id='id' name="uname" type="text" placeholder="ID" onblur="aa(this)"></td>

  </tr>

  <tr>

    <td><input name="pwd" type="password" placeholder="密码"></td>

  </tr>

  <tr>

    <td><input name="" type="submit"></td>

  </tr>

</table>

</form>

<script> 

  function aa(a){

      if(a.value==''){

        a.placeholder='ID不能为空'

       a.className="dd"

      }

  }

</script>

</body>  

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6273741.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-19
下一篇 2023-03-19

发表评论

登录后才能评论

评论列表(0条)

保存