//限制最多字数,由此对象的“maxlength”决定
//使用方法:
//<textarea maxlength="10"></textarea>
function limit_input(tagName)
{
$(tagName + "[maxlength]").live("input", function(){
var mlength = $(this).attr("maxlength");
var mvalue = $(this).val();
if (mvalue.length > mlength) $(this).val(mvalue.substring(0, mlength));
});
$(tagName + "[maxlength]").bind("propertychange", function(){
var mlength = $(this).attr("maxlength");
var mvalue = $(this).val();
if (mvalue.length > mlength) $(this).val(mvalue.substring(0, mlength));
});
}
$(document).ready(function(){
limit_input("textarea");
}); |
//限制最多字数,由此对象的“maxlength”决定
//使用方法:
//<textarea maxlength="10"></textarea>
function limit_input(tagName)
{
$(tagName + "[maxlength]").live("input", function(){
var mlength = $(this).attr("maxlength");
var mvalue = $(this).val();
if (mvalue.length > mlength) $(this).val(mvalue.substring(0, mlength));
});
$(tagName + "[maxlength]").bind("propertychange", function(){
var mlength = $(this).attr("maxlength");
var mvalue = $(this).val();
if (mvalue.length > mlength) $(this).val(mvalue.substring(0, mlength));
});
}
$(document).ready(function(){
limit_input("textarea");
});
受教了!呵呵!