Home > Web Front-end > JS Tutorial > body text

简述JavaScript提交表单的方式 (Using JavaScript Submit Form)_javascript技巧

WBOY
Release: 2016-05-16 15:10:24
Original
1544 people have browsed it

最近做项目遇到用Javascript提交表单的问题, 之前也做过几次, 但是不够全面, 这次总结出了几种用JavaScript提交表单的方式, 并且对此作出了比较, 选出了一种最适合此项目的方式。

我目前正在为Sun Communication Suite做一个创建用户的小型系统,大家都知道我们可以通过表单,Ajax 和链接来访问服务器, 最简单的方法就是使用连接, 例如:First Page, 把所有需要的数据全部写到超链接上, 如果你能够观察一下就会知道,在上边的链接中只有currentPage是变化的, 其他参数event, keyword, searbyBy和cn是不变的, 那么我就想到如果我能够把这些不变的参数封装到一个表单中, 当用户点击上面的超链接的时候我用JavaScript把这个表单提交, 那么我自然会访问到服务器。

表单:

"/> "/> "/>
Copy after login

在提交表单的过程中, 我只需要把参数currentPage传给JavaScript就好了,所以我就把上面的连接改为下边的形式:

["+pages[j]+"]
Copy after login

大家要注意一定要把document.pagination.currentPage.value="+pages[j]+";写在document.pagination.submit();的前边, 这样在用户提交表单之前, 参数currentPage就已经被修改为我们需要的数值。 这样我就完成了用连接来提交表单, 但是我有遇到了一个问题, 我需要试用上面的这段代码在很多页面, 如果我能统一的写一段JavaScript的话,就会方面我以后对整个系统做维护, 所以我几写了一个JavaScript的函数。

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();
}
Copy after login
Copy after login

然后我在超连接的onclick事件上条用这个函数:

["+pages[j]+"], 大家可以看到整段代码简洁了不少。

所以我总结了一下,用Javascript提交表单大概有两种写法(根据我目前的理解)

1. document.formName.submit();

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

form.submit();

下次我想和大家分享一下用JNDI实现分页。我把这次的代码附在下边, 大家可以参考一下。

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();
}
Copy after login
Copy after login

mailingListMemberAdd.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@ 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" %>



Add Member to Mailing List:<%=request.getAttribute("cn")%>


Add new members to mailing list: <%=request.getAttribute("cn")%>

<% Pager pager =(Pager) request.getAttribute("pager"); int currentPage =pager.getCurrentPage(); int pageSize = pager.getPageSize(); int i=(currentPage-1)*pageSize; LinkedList users = (LinkedList)request.getAttribute("users"); if(!users.isEmpty()){ %> <% for(Iterator iter = users.iterator();iter.hasNext();){ User user = (User) iter.next(); i++; // Attributes attrs = user.getAttrs(); %> <% } %> "/>
No. UID:gCode:Givenname:Surname:Mail:Description:
<%=i%>. <%=user.getUid()%> <%=user.getGCode()%> <%=user.getGivenName()%> <%=user.getSn()%> <%=user.getMail()%> <%if(user.getDescription()==null||user.getDescription().length()==0){%>Not Set<%} else %><%=user.getDescription()%>

Pages:

"/> "/> "/>
<% int[] pages = pager.getPages(); String keyword = request.getAttribute("keyword").toString(); String searchBy = request.getAttribute("searchBy").toString(); if(pager.isHasFirst()){ out.println("First Page "); } if(pager.isHasPrevious()){ out.println("Prev Page "); } for(int j=0;j["+pages[j]+"]"); }else { out.println("["+pages[j]+"]"); } } if(pager.isHasNext()){ out.println("Next Page "); } if(pager.isHasLast()){ out.println("Last Page "); } %>

<% } else { //make the mailing list member availabe when user are trying to re-run the search //request.setAttribute("members", members); %>

No results are matched your keyword or the user that you are looking for is already a member of this mailing list, please specify another keywork and ">Search Again

<% } %>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!