0

我有以下代码: //head 部分

<script type="text/javascript">
function check_form()
{
var id = document.forms[0].txtbid.value;
var bname = document.forms[0].txtbtitle.value;
if (id.length==0)
{
    alert("Please enter a valid Book ID.");
    return false;
}
if (id.length>8)
{
    alert("Book ID cannot exceed 8 digit.");
    return false;
}
if (bname.length==0)
{
    alert("Please enter the Book Title.");
    return false;
}
return true;
}

//正文部分:

<form action="addnewbook.php" method="post" onsubmit="return check_form()">
<table border="0" cellspacing="0" cellpadding="5" width="400px"> 
<tr> <td> Book ID </td> <td> <input type="text" name="txtbid" size="30px"> </td> </tr>
<tr> <td> Book Title </td> <td> <input type="text" name="txtbtitle" size="30px"></td>     </tr>
<tr> <td> <input type="reset" value="Clear">
<input type="submit" name="add" value="Add Book"> </td></tr>
</table>
</form>

但是当我单击提交按钮时,会打开 addnewbook.php 而不进行任何客户端验证。之前的某个时候,同一段代码运行良好。我尝试使用“this”关键字对其进行修改,但没有成功。

所以我再次编写了之前的代码,但它没有工作。我重新启动系统认为必须清除缓存。[只是尝试] 但它仍然无法正常工作。我的语法错了吗??

伙计们,我向您发送了简化的代码,它工作正常,完整的代码如下,这不起作用:

<html>
<head>
<link rel="stylesheet" type="text/css" href="../style.css" />
<title>Library Manager | Major Project 2013</title>
<script type="text/javascript">
function check_form()
{
var id = document.forms[0].txtbid.value;
var bname = document.forms[0].txtbtitle.value;
var bauthor = document.forms[0].txtbauthor.value;
var bpub = document.forms[0].txtbpublication.value;
var bedition = document.forms[0].txtbedition.value;
var byear = document.forms[0].txtbyear.value;
var bpages = document.forms[0].check_form.txtbpages.value;
if (id.length==0)
{
alert("Please enter a valid Book ID.");
return false;
}
if (id.length>8)
{
alert("Book ID cannot exceed 8 digit.");
return false;
}
if (bname.length==0)
{
alert("Please enter the Book Title.");
return false;
}
return true;
}
</script>

</head>
<body>
<div id="container">
<?php include 'books_header.php'; ?>
<div id="leftmenu">
<div id="leftmenu_top"></div>
<?php include "books_link.php" ?>
<div id="leftmenu_bottom"></div>
</div>
<div id="content">
<div id="content_top"></div>
<div id="content_main">
<h2> Add New: </h2> Option marked with * are compulsary.
<p>&nbsp;</p>
<form action="addnewbook.php" method="post" onsubmit="javascript:return check_form()">
<table border="0" cellspacing="0" cellpadding="5" width="400px"> 
<tr> <td> Book ID <font id="error"> * </font> </td> <td> <input type="text" name="txtbid" size="30px"> </td> </tr>
<tr> <td> Book Title <font id="error"> * </font></td> <td> <input type="text" name="txtbtitle" size="30px"> </td> </tr>
<tr> <td> Book Author <font id="error"> * </font></td> <td> <input type="text" name="txtbauthor" size="30px"> </td></tr>
<tr> <td> Book Publication </td> <td> <input type="text" name="txtbpublication" size="30px"> </td></tr>
<tr> <td> Edition </td> <td> <input type="text" name="txtbedition" size="30px"> </td></tr>
<tr> <td> Published Year <font id="error"> * </font></td> <td> <input type="text" name="txtbyear" size="30px"> </td></tr>
<tr> <td> Total Pages <font id="error"> * </font></td> <td> <input type="text" name="txtbpages" size="30px"> </td></tr>
<tr> <td> Book Category </td> <td> 
<!-- <input type="text" name="txtbcategory" size="30px"> -->
<select name="txtbcategory"> 
<?php                   
include("../connect.php");    
$query = mysql_query("SELECT * FROM categories");   
while ($row = mysql_fetch_array($query)) {
print "<option value=".$row{'Category'}.">".$row{'Category'}."</option>";
}
mysql_close($dbhandle);
?>                      
</select> 

</td></tr>
<tr> <td> Remark </td> <td> <textarea rows="4" cols="24" name="txtbremark"> </textarea>   </td></tr>
<tr><td>  </td>
<td> <input type="reset" value="Clear">
<input type="submit" name="add" value="Add Book"> </td></tr>
</table>
</form>
<p>&nbsp;</p>
</div>
<div id="content_bottom"></div>
<?php include '../footer.php'; ?>
</div>
</div>
</body>
</html>
4

3 回答 3

0

您的代码在我的系统上运行良好:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>


  <style type='text/css'>

  </style>



<script type='text/javascript'>//<![CDATA[ 
function check_form()
{
var id = document.forms[0].txtbid.value;
var bname = document.forms[0].txtbtitle.value;
if (id.length==0)
{
    alert("Please enter a valid Book ID.");
    return false;
}
if (id.length>8)
{
    alert("Book ID cannot exceed 8 digit.");
    return false;
}
if (bname.length==0)
{
    alert("Please enter the Book Title.");
    return false;
}
return true;
}
//]]>  

</script>


</head>
<body>
  <form action="addnewbook.php" method="post" onsubmit="return check_form()">
<table border="0" cellspacing="0" cellpadding="5" width="400px"> 
<tr> <td> Book ID </td> <td> <input type="text" name="txtbid" size="30px"> </td> </tr>
<tr> <td> Book Title </td> <td> <input type="text" name="txtbtitle" size="30px"></td>     </tr>
<tr> <td> <input type="reset" value="Clear">
<input type="submit" name="add" value="Add Book"> </td></tr>
</table>
</form>

</body>


</html>
于 2013-03-28T13:05:15.993 回答
0

尝试这个:

<form action="addnewbook.php" method="post" onsubmit="javascript:return check_form()">

在表格中。

于 2013-03-28T12:44:18.603 回答
0

你错过了一个 ; 尝试这个:

<form action="addnewbook.php" method="post" onsubmit="return check_form();">
于 2013-03-28T12:47:17.603 回答