一个表单中有编辑和删除两个按钮分别调用后台action的不同方法

本文介绍如何在JSP表单中处理编辑和删除操作,由于表单只有一个submit方法,故采用button结合onclick事件的JavaScript方法来分别调用后台不同Action实现功能。

具体代码实现:

jsp:页面开头:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'bookdesc.jsp' starting page</title>

   .............

  <script type="text/javascript">
    function edit(){
        var aaa = document.getElementById("form1");
        aaa.action="<%=basePath%>/adminBook/edit.action";
        aaa.submit();
    }
    function del(){
        var aaa = document.getElementById("form1");
        aaa.action="<%=basePath%>/adminBook/delete.action";
        aaa.submit();
    }
  </script>
  </head>

  <body>
  <div>
    <img src="<c:url value='/${book.image }'/>" border="0"/>
  </div>
  <form id="form1" action='<c:url value='/adminBook/edit.action'/>' style="margin:20px;" method="post">
    <input type="hidden" name="bid" value="${book.bid }"/>
    <input type="hidden" name="image" value="${book.image }"/>
    图书名称:<input type="text" name="bname" value="${book.bname }"/><br/>
    图书单价:<input type="text" name="price" value="${book.price }"/><br/>
    图书作者:<input type="text" name="author" value="${book.author }"/><br/>
    图书分类:<select style="width: 150px; height: 20px;" name="cid">
    <c:forEach items="${categoryList }" var="c">
         <option value="${c.cid }" <c:if test="${c.cid eq book.category.cid }">selected="selected"</c:if>>${c.cname }</option>
    </c:forEach>        
          </select><br/>
    <input type="button" value="编辑" onclick="edit()"/>
    <input type="button" value="删除" onclick="del()"/> 
  </form>
  </body>
</html>

我们知道一个form表单中只有一个submit方法,所以需要使用两个submit提交肯定是不行的,后来想到使用button的onclick的js方法实现。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值