我有以下代码: //头部
<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> </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> </p>
</div>
<div id="content_bottom"></div>
<?php include '../footer.php'; ?>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
试试这个:
<form action="addnewbook.php" method="post" onsubmit="javascript:return check_form()">
表格。
答案 1 :(得分:0)
你错过了;试试这个:
<form action="addnewbook.php" method="post" onsubmit="return check_form();">
答案 2 :(得分: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>