本文参考:http://my.oschina.net/sallency/blog/300568
在开发工作共我们往往会遇到一个表单需要包含多个action不同的提交动作,这时候就不能在使用submit按钮来进行提交了,下面提供通过js来实现这一功能的方法.
示例代码:
<form name="demo" method="post">
<!--YOUR DATA AREA-->
<input type="button" value="添加" onclick="addAction()">
<input type="button" value="删除" onclick="deleteAction()">
<input type="button" value="保存" onclick="saveAction()">
<input type="button" value="查询" onclick="searchAction()">
</form> <script type="text/javascript">
function addAction(){
document.demo.action="your add method url";
document.demo.submit();
} function deleteAction(){
document.demo.action="your delete method url";
document.demo.submit();
} function saveAction(){
document.demo.action="your add save url";
document.demo.submit();
} function searchAction(){
document.demo.action="your serach method url";
document.demo.submit();
}
</script>
//==================
demo中的代码(JSP中的hidden):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function trimStr(str){
return str.replace(/(^\s*)|(\s*$)/g,"");
}
function redisSearchSubmit(){
document.getElementById("searchtype").value = "Redis查询";
document.form1.submit();
} function jdbcSearchSubmit(){
document.getElementById("searchtype").value = "JDBC查询Mysql";
document.form1.submit();
} function hibernateSearchSubmit(){
document.getElementById("searchtype").value = "Hibernate查询Mysql";
document.form1.submit();
}
function check(){ // 节目名
var assetname = document.form1.assetname.value;
if(trimStr(assetname) == ""){
alert("您输入的节目名是空格,请重新输入");
return false;
} // 导演
var director = document.form1.director.value; // 编剧
var screenwriter = document.form1.screenwriter.value; //主演
var actor = document.form1.actor.value;
} </script>
</head>
<body>
<h1>查询页面</h1>
<FORM name="form1" METHOD=POST ACTION="${ pageContext.request.contextPath }/redisSqlSearch" onsubmit="return check();">
<input type="hidden" name="searchtype" id="searchtype" value="" />
<TABLE border="1" width="500">
<TR>
<TD>节目名称</TD>
<TD><INPUT TYPE="text" NAME="assetname" value=""></TD>
</TR>
<TR>
<TD>导演</TD>
<TD><INPUT TYPE="text" NAME="director" value="冯小刚"></TD>
</TR>
<TR>
<TD>编剧</TD>
<TD><INPUT TYPE="text" NAME="screenwriter" value="顾晓阳"></TD>
</TR>
<TR>
<TD>主演</TD>
<TD><INPUT TYPE="text" NAME="actor" value="葛优"></TD>
</TR>
<TR>
<TD colspan="2">
<INPUT TYPE="button" value="Redis查询" onclick="redisSearchSubmit()"> <INPUT TYPE="button" value="JDBC查询Mysql" onclick="jdbcSearchSubmit()"> <INPUT TYPE="button" value="Hibernate查询Mysql" onclick="hibernateSearchSubmit()">
</TD>
</TR>
</TABLE>
</FORM>
</body>
</html>