公主日记2最后和谁结婚:javascript文本框得到光标的问题

来源:百度文库 编辑:高考问答 时间:2024/04/25 22:40:34
<form name="form1" method="post" action="">
<label>
<input type="submit" name="Submit" value="提交" onClick="check()">
</label>
<label>sd
<input name="name" type="text" id="name">
</label>
<label>
<input name="pwd" type="text" id="pwd">
</label>
</form>
<script language="JavaScript">
function check()
{
if (document.form1.name.value=="")
{
alert("请输入姓名!");
document.form1.name.focus();
return false;
}
else if (document.form1.pwd.value=="")
{
alert("请输入密码!")
document.form1.pwd.focus()
return false
}
}

</script>

我的代码有什么错误为什么每次执行后文本框都不能得到光标而只是刷新下页面

<script language="JavaScript">
function check()
{
if (document.form1.name.value=="")
{
alert("请输入姓名!");
document.form1.name.focus();
return false;
}
else if (document.form1.pwd.value=="")
{
alert("请输入密码!");
document.form1.pwd.focus();
return false ;
}
}

</script>
<form name="form1" method="post" action="" onsubmit="return check()">
<label>
<input type="submit" name="Submit" value="提交"">
</label>
<label>sd
<input name="name" type="text" id="name">
</label>
<label>
<input name="pwd" type="text" id="pwd">
</label>
</form>

以上代码可以运行!
你的提交表单,所以表头要用
<form name="form1" method="post" action="" onsubmit="return check()">
这样在提交的时候就会检查,符合条件就可以提交出去!

<input type="submit" name="Submit" value="提交" onClick="check()">

把这里的type 中的submit 改成 button

submit 是 提交按钮
button 是普通的 ...

onfocus事件

<input type="submit" name="Submit" value="提交"
onClick="return check();">看这了吗?return check();这么写才可以,要加分号
还有name是关键字吧,换一个别的写就可以了

jiushixuefeng 这位大哥,怎么要加分号啊!???
有没有学过JS??