如何处理jsp文件中的两个提交按钮

时间:2018-02-20 10:21:59

标签: jsp servlets

我有一个带有两个提交按钮的jsp页面,如下所示

 <form name="myForm" action="app" method="post">
    <select name="selection" multiple="multiple" id="select" required>
        <option value="">Select Languages....</option>
        <option value="nl_value">Dutch</option>
        <option value="en_string">English</option>
        <option value="fr_value">French</option>
        <option value="de_value">German</option>
        <option value="es_value">Spanish</option>
    </select> <br> <br> <br> 
    <input type="submit" name="translate" value="Translate"> 
    <input type="submit" name="tableData" value="GetFullStringsContent">
</form>

对于第一个提交按钮,必须在下拉列表中选择一个项目,以便我根据需要保留<select>标记,并且对于下一个提交按钮,它是可选的

提交后

将转到

 @WebServlet(value = "/app")
 public class LoginServlet extends HttpServlet {

 private static final long serialVersionUID = 1L;

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    if (req.getParameter("tableData") != null) {
        RequestDispatcher rd = req.getRequestDispatcher("application.jsp");
        rd.forward(req, resp);
    } else if (req.getParameter("translate") != null) {
        String[] str = req.getParameterValues("selection");
        String columns = "";
 //next there is some logic

但是当我点击第二个按钮时,请求不会转发到所需的jsp。给我一个解决方案

1 个答案:

答案 0 :(得分:0)

您可以在onclickGetFullStringsContent并在该功能中删除&#34; required&#34;单击select

&#13;
&#13;
function test(){
  $("select[name=selection]").removeAttr('required');
};
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="myForm" action="app" method="post">
    <select name="selection" multiple="multiple" id="select" required>
        <option value="">Select Languages....</option>
        <option value="nl_value">Dutch</option>
        <option value="en_string">English</option>
        <option value="fr_value">French</option>
        <option value="de_value">German</option>
        <option value="es_value">Spanish</option>
    </select> <br> <br> <br> 
    <input type="submit" name="translate" value="Translate"> 
    <input type="submit" onclick="test()" name="tableData" value="GetFullStringsContent">
</form>
&#13;
&#13;
&#13;