近期在写一个购票系统,发现form表单的post请求时不做判断就直接提交了,以前一直没注意,好像这个是系统默认行为,可通过在form表单上添加一个对象事件onsubmit
,也就是提交表之前调用加以判断是否提交
例:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="layui/css/layui.css" rel="stylesheet" />
<script src="layui/layui.js"></script>
<script>
//条件判断
function ConditionJudge() {
var value = document.getElementById("txtTest").value;
if (value == "")
return false;
else
return true;
}
</script>
</head>
<body>
<form id="form1" onsubmit="return ConditionJudge()" action="Test1.aspx" method="post" runat="server">
<div>
<asp:TextBox ID="txtTest" runat="server"></asp:TextBox>
<input type="submit" class="layui-btn" name="name" value="提交" />
</div>
</form>
</body>
</html>