简述JavaScript提交表单的方式 (Using JavaScript Submit Form)
最近做项目遇到用Javascript提交表单的问题,之前也做过几次,但是不够全面,这次总结出了几种用JavaScript提交表单的方式,并且对此作出了比较,选出了一种最适合此项目的方式。
我目前正在为SunCommunicationSuite做一个创建用户的小型系统,大家都知道我们可以通过表单,Ajax和链接来访问服务器,最简单的方法就是使用连接,例如:<ahref=UserServlet?event=SEARCH_MAILING_LIST¤tPage=1&keyword="+keyword+"&searchBy="+searchBy+"&cn="+request.getAttribute("cn")+">FirstPage</a>,把所有需要的数据全部写到超链接上,如果你能够观察一下就会知道,在上边的链接中只有currentPage是变化的,其他参数event,keyword,searbyBy和cn是不变的,那么我就想到如果我能够把这些不变的参数封装到一个表单中,当用户点击上面的超链接的时候我用JavaScript把这个表单提交,那么我自然会访问到服务器。
表单:
<formname="pagination"id="pagination"action="UserServlet"method="get"> <inputtype="hidden"name="currentPage"value="1"/> <Inputtype="hidden"name="cn"value="<%=request.getAttribute("cn")%>"/> <inputtype="hidden"name="keyword"value="<%=request.getAttribute("keyword")%>"/> <inputtype="hidden"name="searchBy"value="<%=request.getAttribute("searchBy")%>"/> <inputtype="hidden"name="event"value="SEARCH_USER_FOR_MAILING_LIST"> </form>
在提交表单的过程中,我只需要把参数currentPage传给JavaScript就好了,所以我就把上面的连接改为下边的形式:
<ahref=#onclick=document.pagination.currentPage.value="+pages[j]+";document.pagination.submit();><spanstyle='color:red;'>["+pages[j]+"]</span></a>
大家要注意一定要把document.pagination.currentPage.value="+pages[j]+";写在document.pagination.submit();的前边,这样在用户提交表单之前,参数currentPage就已经被修改为我们需要的数值。这样我就完成了用连接来提交表单,但是我有遇到了一个问题,我需要试用上面的这段代码在很多页面,如果我能统一的写一段JavaScript的话,就会方面我以后对整个系统做维护,所以我几写了一个JavaScript的函数。
functionsubmitForm(id,currentPage){ //varcurrentPage=document.pagination.currentPage.value; //alert(currentPage); //currentPage=100; //varcurrentPage=document.pagination.currentPage.value; //alert(currentPage); document.pagination.currentPage.value=currentPage; varform=document.getElementById(id); form.submit(); }
然后我在超连接的onclick事件上条用这个函数:
<ahref=#onclick=submitForm('pagination',"+pages[j]+")>["+pages[j]+"]</a>,大家可以看到整段代码简洁了不少。
所以我总结了一下,用Javascript提交表单大概有两种写法(根据我目前的理解)
1.document.formName.submit();
2.varform=document.getElementById(id);
form.submit();
下次我想和大家分享一下用JNDI实现分页。我把这次的代码附在下边,大家可以参考一下。
commons.js
functionsubmitForm(id,currentPage){ //varcurrentPage=document.pagination.currentPage.value; //alert(currentPage); //currentPage=100; //varcurrentPage=document.pagination.currentPage.value; //alert(currentPage); document.pagination.currentPage.value=currentPage; varform=document.getElementById(id); form.submit(); }
mailingListMemberAdd.jsp
<%@pagecontentType="text/html"pageEncoding="UTF-8"%> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@pageimport="java.util.LinkedList"%> <%@pageimport="java.util.Iterator"%> <%@pageimport="java.util.ArrayList"%> <%@pageimport="java.util.List"%> <%@pageimport="my.gov.rmp.webmail.domain.User"%> <%@pageimport="my.gov.rmp.webmail.util.Pager"%> <html> <head> <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"> <title>AddMembertoMailingList:<%=request.getAttribute("cn")%></title> <scripttype="text/javascript"src="../js/commons.js"></script> </head> <body> <jsp:includepage="../inc/admin/headerMail.jsp"/> <divid="main"> <divid="contents"> <h2>Addnewmemberstomailinglist:<spanstyle="color:blue;"><%=request.getAttribute("cn")%></span></h2> <formname="addMailingListMember"id="addMailingListMember"action="UserServlet"method="post"> <tablecellspacing="5"cellpadding="5"> <% Pagerpager=(Pager)request.getAttribute("pager"); intcurrentPage=pager.getCurrentPage(); intpageSize=pager.getPageSize(); inti=(currentPage-1)*pageSize; LinkedListusers=(LinkedList)request.getAttribute("users"); if(!users.isEmpty()){ %> <trstyle="font-weight:bold"><td>No. </td><td>UID:</td><td>gCode:</td><td>Givenname:</td><td>Surname:</td><td>Mail:</td><td>Description:</td></tr> <% for(Iteratoriter=users.iterator();iter.hasNext();){ Useruser=(User)iter.next(); i++; //Attributesattrs=user.getAttrs(); %> <tr><td><%=i%>. <inputtype="checkbox"name="uid"value="<%=user.getUid()%>"/></td> <tdwidth="15%"><%=user.getUid()%></td> <td><%=user.getGCode()%></td> <td><%=user.getGivenName()%></td> <td><%=user.getSn()%></td> <tdwidth="30%"><%=user.getMail()%></td> <td><%if(user.getDescription()==null||user.getDescription().length()==0){%>NotSet<%}else%><%=user.getDescription()%></td> </tr> <% } %> <inputtype="hidden"name="cn"value="<%=request.getParameter("cn")%>"/> <inputtype="hidden"name="event"value="ADD_MAILING_LIST_MEMBER"/> <tr><td><buttononclick="returnselectAllCheckBoxs('uid');">SelectAll</button></td><td><buttononclick="returndeselectAllCheckBoxs('uid')">DeselectAll</button></td><td></td><td><inputtype="submit"name="submit"value="AddtoMailingList"/></td></tr> </table> </form> <hr> <PSTYLE="margin-top:-5px;"><strong>Pages:</strong> <formname="pagination"id="pagination"action="UserServlet"method="get"> <inputtype="hidden"name="currentPage"value="1"/> <Inputtype="hidden"name="cn"value="<%=request.getAttribute("cn")%>"/> <inputtype="hidden"name="keyword"value="<%=request.getAttribute("keyword")%>"/> <inputtype="hidden"name="searchBy"value="<%=request.getAttribute("searchBy")%>"/> <inputtype="hidden"name="event"value="SEARCH_USER_FOR_MAILING_LIST"> </form> <% int[]pages=pager.getPages(); Stringkeyword=request.getAttribute("keyword").toString(); StringsearchBy=request.getAttribute("searchBy").toString(); if(pager.isHasFirst()){ out.println("<ahref=UserServlet?event=SEARCH_USER_FOR_MAILING_LIST¤tPage=1&keyword="+keyword+"&searchBy="+searchBy+"&cn="+request.getAttribute("cn")+">FirstPage</a> "); } if(pager.isHasPrevious()){ out.println("<ahref=#onclick=submitForm('pagination',"+(pager.getCurrentPage()-1)+")>PrevPage</a> "); } for(intj=0;j<pages.length;j++){ if(pager.getCurrentPage()==pages[j]){ out.println("<ahref=#onclick=document.pagination.currentPage.value="+pages[j]+";document.pagination.submit();><spanstyle='color:red;'>["+pages[j]+"]</span></a>"); }else{ out.println("<ahref=#onclick=submitForm('pagination',"+pages[j]+")>["+pages[j]+"]</a>"); } } if(pager.isHasNext()){ out.println("<ahref=#onclick=submitForm('pagination',"+(pager.getCurrentPage()+1)+")>NextPage</a> "); } if(pager.isHasLast()){ out.println("<ahref=#onclick=submitForm('pagination',"+pager.getTotalPage()+")>LastPage</a> "); } %> </P> <% }else{ //makethemailinglistmemberavailabewhenuseraretryingtore-runthesearch //request.setAttribute("members",members); %> <p>Noresultsarematchedyourkeywordortheuserthatyouarelookingforisalreadyamemberofthismailinglist,pleasespecifyanotherkeyworkand<ahref="<%=request.getContextPath()%>/admin/mailingListMemberSearch.jsp?cn=<%=request.getParameter("cn")%>">SearchAgain</a></p> <% } %> </div> </div> </body> </html>