<head>
<title>无标题文档</title>
<script language="javascript">
function abc()
{
if(form1.textfield.value=="")
{
window.alert("用户名不能为空!");
form1.textfield.focus();
}
}
</script>
</head><body>
<form id="form1" name="form1" method="post" action="">
<table width="263" height="121" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="70">用户名:</td>
<td width="168"><label>
<input type="text" name="textfield" />
</label></td>
</tr>
<tr>
<td>密 码:</td>
<td><label>
<input type="text" name="textfield2" />
</label></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<input type="submit" name="Submit" value="提交" onclick="abc()" />
<input type="submit" name="Submit2" value="清空" />
</div>
</label></td>
</tr>
</table>
</form>
</body>
</html>代码如上所示,当用户名为空时,点提交按钮时无法在用户名文本框获取焦点,请各位大侠指教,谢谢!
改成<input type="button" name="Submit" value="提交" onclick="abc()" /> 既然又click事件,那就不用submit,这样就能获得焦点。
再在
function abc()
{
if(form1.textfield.value=="")
{
window.alert("用户名不能为空!");
form1.textfield.focus();
form1.submit();
}
}