问题 怎么向setTimeout传递参数?????望高手指点
//限制输入字符的个数
//参数:type控件类型 obj控件ID maxLength最大的长度
function MaxInputLength(type,obj,maxLength)
{
window.setTimeout("MaxInputLength("+ type +"," + obj +"," + maxLength+ ");",100);
var inputVal=getid(type+obj).value;
if(inputVal.length>maxLength)
{
inputVal=inputVal.substring(0,maxLength);
}
else{
$("#"+obj).html(maxLength-inputVal.length);
}
}
调用:<asp:TextBox ID="txtMaxinput" runat="server" TextMode="MultiLine" MaxLength="90" onkeydown="MaxInputLength('txt','Maxinput','90')" onkeyup="MaxInputLength('txt','Maxinput','90')"></asp:TextBox>你还可以输入<span id="Maxinput">90</span>个字符
错误信息:txt未定义!
setTimeout(function(){alert(msg)},1000)
}匿名函数+闭包
<body>
<script type="text/javascript">
<!--
function getid(id){return document.getElementById(id)}
function MaxInputLength(type,obj,maxLength)
{
//window.setTimeout("MaxInputLength("+ type +"," + obj +"," + maxLength+ ");",100);
var inputVal=getid(type+obj).value;
if(inputVal.length>maxLength)
{
getid(type+obj).value=inputVal.substring(0,maxLength);
}
else{
$("#"+obj).html(maxLength-inputVal.length);
}
}//-->
</script>
<input ID="txtMaxinput" TextMode="MultiLine" MaxLength="90" onkeydown="MaxInputLength('txt','Maxinput','5')" onkeyup="MaxInputLength('txt','Maxinput','5')"/>你还可以输入<span id="Maxinput">5</span>个字符
</body>
window.setTimeout("MaxInputLength('"+ type +"','" + obj +"'," + maxLength+ ");",100);