php 验证表单的时候 onsubmit 不执行 自己写的javascript函数

<html>
<head>
<?php
header("Content-type: text/html; charset=gbk2132");
$incPath = dirname("_FILE_");
require_once "{$incPath}/include/db_IpAddr/ipaddress.php";
require_once "{$incPath}/include/DataUse.php";
require_once "{$incPath}/messagetopic/MtoP.php";
if($_POST['btnTiJiao'])
{
$du=new DataUse();
$addr=ipCity($_SERVER['REMOTE_ADDR']);
$time=date("YmdHis");
$mp=new MtoP();
$ImgUrl=$mp->getPic($_POST["txtUserName"],$_POST["txtMessage"],$time,$addr);
$sql="insert into tblmessage (Ip,User,Message,Time,ImgUrl) " .
"values ('{$_SERVER['REMOTE_ADDR']}','$_POST[txtUserName]','$_POST[txtMessage]','{$time}','{$ImgUrl}')";
$du->query($sql);

echo "<script language=\"javascript\">alert('添加成功');history.go(-1)</script>";

}
?>
<script type="javascript">
function chk()
{
if(form_addMessage.txtUserName.value=="")
{
alert("留言人不能为空");
form_addMessage.txtUserName.focus();
return false;
}
else if(form_addMessage.txtUserName.value.length>5)
{
alert("你名字有这么长吗");
form_addMessage.txtUserName.select();
form_addMessage.txtUserName.focus();
return false;
}
if(form_addMessage.txtMessage.value=="")
{
alert("内容不能为空");
form_addMessage.txtMessage.focus();
return false;
}
else if(form_addMessage.txtMessage.value.length>60)
{
alert("精简一下祝福吧!");
form_addMessage.txtMessage.select();
form_addMessage.txtMessage.focus();
return false;
}
return false;
}

</script>
<title>送祝福啦!</title>
</head>
<form name="form_addMessage" id="form_addMessage" method="post" action="test.php" onsubmit="return chk();" >
<body>
<div style="width:980px;margin:0px auto;">
<table border="2">
<tr>
<td colspan="2" align="center">增加祝福话语</td>
</tr>
<tr>
<td style="width:200px">用户Ip</td>
<td><?php echo $_SERVER['REMOTE_ADDR']; ?></td>
</tr>
<tr>

你的form在哪里??你可以以form中添加属性onsubmit="return chk()"

<form action="" onsubmit="return chk()">///
//js代码最好不用用id.method这种方式,这种方式不够兼容。
//可以这样试试。
function chk(){
    var form_addMessage = document.getElementById('form_addMessage');
//接下来就是你的js代码了。
}

追问

有了的,只是贴的时候贴掉了

追答<form action="" onsubmit="return chk()">///
//js代码最好不用用id.method这种方式,这种方式不够兼容。
//可以这样试试。
function chk(){
    var form_addMessage = document.getElementById('form_addMessage');
//接下来就是你的js代码了。
}

追问

按照你的做法弄,还是不执行啊,我直接
function chk()
{
alert('123');
return false;
}

这样都不执行

追答

会用firebug吗?你看一下这个有没有代码错误之类的。

追问

这样都不行了

追答

你没发现你代码错了吗???html代码的层次如下。

<html>
<head></head>
<body>
    <form></form>
</body>
</html>

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-21
你的javascript onclick 函数定义了吗?没有定义javascript当然不能执行了啊!你需要定义onclick 和onmouse时间函数!
第2个回答  2013-06-21
语法错误
onsubmit="javascript:return chk();"
第3个回答  2013-06-21
代码写的相当不规范呐。。。
</head> <form> <body> 我都没看见
重点是没有<from>才导致submit失效的吧追问

有了的,只是贴的时候贴掉了

第4个回答  2013-06-21
没看到你的onsubmit属性的定义
好像你都没有form的开始标签追问

有了的,只是贴的时候贴掉了