Briefly describe the JavaScript Form submission method (Using JavaScript Submit Form), Using criptusing

Source: Internet
Author: User

Briefly describe the JavaScript Form submission method (Using JavaScript Submit Form), Using criptusing

I recently encountered the problem of submitting forms using Javascript in my project. I have done it several times before, but it is not comprehensive enough. I have summarized several methods of submitting forms using JavaScript and compared them, select a method that best fits the project.

I am currently working on a small User Creation System for Sun Communication Suite. We all know that we can access the server through forms, Ajax and links. The simplest way is to use connections, for example: <a href = UserServlet? Event = SEARCH_MAILING_LIST & currentPage = 1 & keyword = "+ keyword +" & searchBy = "+ searchBy +" & cn = "+ request. getAttribute ("cn") + "> First Page </a>: writes all the required data to the hyperlink. If you can observe it, you will know, in the above link, only currentPage is changed. Other parameters such as event, keyword, searbyBy, and cn are unchanged, then I thought that if I could encapsulate these unchanged parameters in a form, I would use JavaScript to submit the form when the user clicks the hyperlink above, then I will naturally access the server.

Form:

<form name="pagination" id="pagination" action="UserServlet" method="get"><input type="hidden" name="currentPage" value="1"/><Input type="hidden" name="cn" value="<%=request.getAttribute("cn")%>"/><input type="hidden" name="keyword" value="<%=request.getAttribute("keyword")%>"/><input type="hidden" name="searchBy" value="<%=request.getAttribute("searchBy")%>"/><input type="hidden" name="event" value="SEARCH_USER_FOR_MAILING_LIST"></form>

During the form submission process, I only need to pass the currentPage parameter to JavaScript, so I changed the above connection to the following form:

<a href=# onclick=document.pagination.currentPage.value="+pages[j]+";document.pagination.submit();><span style='color: red;'>["+pages[j]+"]</span></a>

You must note that the document. pagination. currentPage. value = "+ pages [j] +"; written in document. pagination. submit ();, so that before the user submits the form, the parameter currentPage has been changed to the value we need. In this way, I have used the connection to submit the form, but I have encountered a problem. I need to try the above Code on many pages. If I can write a piece of JavaScript in a unified way, I will maintain the entire system in the future, so I have written a JavaScript function.

function submitForm(id,currentPage){//var currentPage = document.pagination.currentPage.value;//alert(currentPage);//currentPage=100;//var currentPage = document.pagination.currentPage.value;//alert(currentPage);document.pagination.currentPage.value=currentPage;var form = document.getElementById(id);form.submit();}

Then I will use this function on the superjoin onclick event:

<A href = # onclick = submitForm ('pagination', "+ pages [j] +")> ["+ pages [j] +"] </a>, you can see that the entire code segment is much simpler.

So I summarized it and usedThere are two ways to write a Javascript submission form:(Based on my current understanding)

1. document. formName. submit ();

2. var form = document. getElementById (id );

Form. submit ();

Next time I want to share with you how to implement paging using JNDI. I have attached the code below for your reference.

Commons. js

function submitForm(id,currentPage){//var currentPage = document.pagination.currentPage.value;//alert(currentPage);//currentPage=100;//var currentPage = document.pagination.currentPage.value;//alert(currentPage);document.pagination.currentPage.value=currentPage;var form = document.getElementById(id);form.submit();}

MailingListMemberAdd. jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><%@ page import="java.util.LinkedList" %><%@ page import="java.util.Iterator" %><%@ page import="java.util.ArrayList" %><%@ page import="java.util.List" %><%@ page import="my.gov.rmp.webmail.domain.User" %><%@ page import="my.gov.rmp.webmail.util.Pager" %>

Articles you may be interested in:
  • Js simulated click to submit a form as an example to be compatible with mainstream browsers
  • Summary of three methods for submitting forms after js validation form
  • Jquery serialized form json data returned after ajax submission
  • Cause of error that form. submit () cannot be submitted in JS
  • Js cursor location text box carriage return form submission Solution
  • Javascript implements the keyboard Enter key to submit forms
  • Detailed analysis of how to submit a form in AngularJS Programming
  • Js implementation of a label hyperlink to submit form Method
  • Use javascript to submit form method summary
  • How does jquery ajax submit form data to jsp?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.