javascript 如何使提交和保存按钮在jsp页面上工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21925954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:01:04  来源:igfitidea点击:

How to make submit and save button work on jsp page?

javascripthtmlformsjsp

提问by user3300851

I am creating a form..i want my form to have two buttons one for "submit" and the other for "Save as draft".when I click on submit the data has to be submitted and when I click on save it should show the filled form on another page.I know how to submit and save independently but how do I make them work in the same form.my code is as follows

我正在创建一个表单。另一页上填写的表格。我知道如何独立提交和保存,但如何让它们在同一表格中工作。我的代码如下

<form method="post" action="submitpage.jsp" >//I want here in action to change according to the button clicked
<table border="2" >
<tr>
<td>Field 1</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field1"  id="F1"/></td>
</tr>
<tr>
<td>Field 2</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field2"  id="F2" /></td>
</tr>
<tr>
<td>Field 3</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field3"  id="F3" /></td>
</tr>
<td>Field 4</td>
<td><input  type="text" style=" width: 150px; height: 15px" value="" name="Field4"  id="F4" /></td>
</tr>


<tr>
<td></td>
<td><input type="submit" value="Submit" onclick="return js_file_for_validation()" autofocus="autofocus"/></td>
<td><input type="button" value="Save as draft" /></td>
</tr>
</table></form>

</form>

P.s:-I AM USING JAVASCRIPT FOR VALIDATING SUBMIT!!!! IS there anything that I have to write in js file ??? Guidance will be helpful!!

Ps:-我正在使用 JAVASCRIPT 来验证提交!!!!有什么我必须在js文件中写的吗???指导会有帮助!!

Thanks in adv!!

感谢广告!!

采纳答案by Santhosh

You should write another javascript function and map it to the onclick event of save button

您应该编写另一个javascript函数并将其映射到保存按钮的onclick事件

<td><input type="button" onclick="return saveForm();" value="Save as draft" /></td>

And the java script method as ,

而java脚本方法为,

Java script

Java脚本

function saveForm(){
document.form[0].submit;
}

In your servlet get the attributes and show your filled form.

在您的 servlet 中获取属性并显示您填写的表单。

Hope it helps!!

希望能帮助到你!!

回答by Standin.Wolf

Try this

试试这个

<td><input type="submit" name="submit" value="Submit" onclick="return js_file_for_validation()" autofocus="autofocus"/></td>
<td><input type="submit" name="submit1" value="Save as draft" /></td>

At servlet.java

在 servlet.java

if (request.getParameter("submit") != null) {
 //add values to the database  

} else if (request.getParameter("submit1") != null) {
      //save as draft button is clicked
}

回答by jmail

The save operation is following as:

保存操作如下:

this servlet page:

这个servlet页面:

    public class UserController extends HttpServlet 
    {
        private static final long serialVersionUID = 1L;
        private static String DEP_EDIT="/editdepartment.jsp";
    //other details
    }

    protected void dotGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException 
    {
  if(action.equalsIgnoreCase("depedit"))
    {
        forward=DEP_EDIT;
        int dep_Id=Integer.parseInt(request.getParameter("dep_Id"));
        User user=dao.getUserById(dep_Id);
        request.setAttribute("user", user);
    }  //other operation
    }

this is save.jsp page:

这是 save.jsp 页面:

  <form method="post" action='UserController?action=depedit' >

            Department ID : <input type="text" name="dep_id"
                value="<c:out value="${user.dep_id}" />" /> <br />
            Employee Department: <input type="text" name="depName" 
                value="<c:out value="${user.depName}"/>"/><br/>

            <input type="submit" value="save"/>

And the submit is same as save, just look at that code and following links.

提交与保存相同,只需查看该代码和以下链接即可。

   public class UserController extends HttpServlet 
    {
        private static final long serialVersionUID = 1L;
        private static String DEP_LIST="/departmentlist.jsp";
    //other details
    }

    protected void dotGet(HttpServletRequest request, 
       HttpServletResponse response) throws ServletException, IOException 
{
if(action.equalsIgnoreCase("departmentlist"))
    {
        forward=DEP_LIST;
        request.setAttribute("users", dao.getAll());
    }
} 

this is submit.jsp page:

这是 submit.jsp 页面:

 <form method="post" action='UserController?action=depedit' >

            Department ID : <input type="text" name="dep_id"
                value="<c:out value="${user.dep_id}" />" /> <br />
            Employee Department: <input type="text" name="depName" 
                value="<c:out value="${user.depName}"/>"/><br/>

            <input type="submit" value="SUBMIT"/>

click this link to get the all type of submit buttons and full details

单击此链接以获取所有类型的提交按钮和完整详细信息

  1. https://stackoverflow.com/a/21927496/3242978
  1. https://stackoverflow.com/a/21927496/3242978