Briefly describe the way JavaScript submits a form (Using JavaScript submit form) _javascript Tips

Source: Internet
Author: User

Recently, the project encountered the problem of submitting a form with JavaScript, a few times before, but not comprehensive, this time summed up several ways to submit the form in JavaScript, and compared it, chose one of the most appropriate way for this project.

I am currently working on a small system for creating users for the Sun Communication Suite, and we all know that we can access the server through forms, Ajax, and links, the easiest way is to use the connection, for example: <a href=userservlet? Event=search_mailing_list&currentpage=1&keyword= "+keyword+" &searchby= "+searchBy+" &cn= "+ Request.getattribute ("cn") + ">first Page</a>, write all the required data to the hyperlink, and if you can observe, you will know that only the currentpage in the top link is changed, Other parameters event, keyword, searbyby and CN are invariant, then I think if I can encapsulate these invariant parameters into a form, I will naturally access the server if I submit the form in JavaScript when the user clicks the hyperlink above.

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 "
Hidden "Name=" event "value=" Search_user_for_mailing_list ">
</form>

In the process of submitting the form, I just need to pass the parameter currentpage to JavaScript, so I'll change the above connection to the following form:

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

We must pay attention to the document.pagination.currentpage.value= "+pages[j]+", written in the front of Document.pagination.submit (), so that before the user submits the form, The parameter currentpage has been modified to the value we need. So I finished using the connection to submit the form, but I have a problem, I need to try this piece of code on a lot of pages, if I can write a unified JavaScript, I will later on the whole system to do maintenance, so I wrote a few JavaScript functions.

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 use this function on the hyper-connected onclick event:

<a href=# onclick=submitform (' pagination ', "+pages[j]+") >["+pages[j]+"]</a>, you can see that the whole piece of code is a lot simpler.

So I've concluded that there are probably two ways to use JavaScript to submit forms (based on my current understanding)

1. Document.formName.submit ();

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

Form.submit ();

Next time I want to share it with you. Implement pagination with Jndi. I attached this code to the bottom, we can refer to.

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"%> 
Related Article

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.