/**
* 为form中的文本框绑定验证最大长度事件(按字节)
*
* @param fromName
*/
function bindMaxLength(fromName,height){
if(typeof(height) == "undefined"){
height = 16;
}
$("form[name="+fromName+"] input[type=text]").each(function(){
if(typeof ($(this).attr("maxlength"))!="undefined"){
// alert("width"+this.style.width);
// alert("width"+$(this).css("width")); //会取到默认宽度
//解决ie8下onpropertychange事件间歇性失效的问题
if(this.style.width != "" && this.style.height==""){
this.style.height = height+"px";
}
$(this).bind('input propertychange', function() {
var inputLength = this.value.replace(/[^\x00-\xff]/g, 'xxx').length;
var maxlength = parseInt($(this).attr("maxlength"));
if(inputLength > maxlength){
this.value = this.value.subStringByBytes(maxlength);
}
});
}
});
}
subStringByBytes
JS按字节计算字符串长度,按字节截取字符串
http://happyqing.iteye.com/blog/1979816