上代码:下面的代码会出现俩个文本框焦点不停的互换的问题,死循环。原因我也清楚,想问问大家有没有什么针对这种情况好的解决方案,目前就俩个文本框 ,可能是多个文本框。
<html>
<head>
<title></title> <script language="javascript" type="text/javascript"> function CheckValue(senders) { if (senders.value.length == 0 || senders.value.match(/^\s*$/)) {
window.alert(senders.id+"不能为空!");
senders.focus();
return;
} }
</script></head>
<body>
<form id="form1" runat="server">
<center>
<table>
<tr>
<td>
Name:
</td>
<td>
<input type="text" id="txtName" onblur="CheckValue(this);" />
</td>
</tr>
<tr>
<td>
Address:
</td>
<td>
<input type="text" id="txtAddress" onblur="CheckValue(this);" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" id="btnCommit" value="提交" />
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
<html>
<head>
<title></title> <script language="javascript" type="text/javascript"> function CheckValue(senders) { if (senders.value.length == 0 || senders.value.match(/^\s*$/)) {
window.alert(senders.id+"不能为空!");
senders.focus();
return;
} }
</script></head>
<body>
<form id="form1" runat="server">
<center>
<table>
<tr>
<td>
Name:
</td>
<td>
<input type="text" id="txtName" onblur="CheckValue(this);" />
</td>
</tr>
<tr>
<td>
Address:
</td>
<td>
<input type="text" id="txtAddress" onblur="CheckValue(this);" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" id="btnCommit" value="提交" />
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
<head>
<title></title> <script language="javascript" type="text/javascript"> function CheckValue(senders) { if (senders.value.length == 0 || senders.value.match(/^\s*$/)) {
senders.focus();
window.alert(senders.id+"不能为空!");
return false;
} }
</script></head>
<body>
<form id="form1" runat="server">
<center>
<table>
<tr>
<td>
Name:
</td>
<td>
<input type="text" id="txtName" onblur="CheckValue(this);" />
</td>
</tr>
<tr>
<td>
Address:
</td>
<td>
<input type="text" id="txtAddress" onblur="CheckValue(this);" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="button" id="btnCommit" value="提交" />
</td>
</tr>
</table>
</center>
</form>
</body>
</html>
或者在onsumit里面再检查
senders.focus();
换成:
senders.focus();
window.alert(senders.id+"不能为空!");再觉得不妥 那就onsubmit一块验证吧