js打开新窗口并且POST传入参数
生活随笔
收集整理的這篇文章主要介紹了
js打开新窗口并且POST传入参数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先新建一個index.jsp,在里面添加一個form表單,和一個button。在form中設置method為POST,并且設置好action為需要跳轉的jsp頁面,target參數設為_blank,表示新打開一個窗口。然后為button添加響應事件,調用form的submit方法。index.jsp代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script type="text/javascript">function newPage(){//var name=document.getElementById("name");//var pwd=document.getElementById("pwd");var newPage=document.getElementById("newPageForm");newPage.submit(); //提交實現跳轉}</script></head><body><form id="newPageForm" action="newPage.jsp" target="_blank" method="POST"><label for="name">用戶:</label><input type="text" id="name" name="name"/><br><label for="pwd">密碼:</label><input type="text" id="pwd" name="password"/></form><button onClick="newPage()">打開新頁面</button></body> </html>
在要跳轉的jsp頁面中,添加兩個type="hidden"的input用來接收傳過來的參數,并且用js讀取那兩個input的值,然后在lable中顯示出來,newPage.jsp代碼如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript">function setLabel(){var theLabel=document.getElementById("content");var name=document.getElementById("name").value;var pwd=document.getElementById("pwd").value;var content="我是"+name+"密碼為"+pwd;theLabel.innerHTML=content;} </script> </head> <body οnlοad="setLabel()"><label id="content"></label><input type="hidden" id="name" value="<%=request.getParameter("name")%>" /><input type="hidden" id="pwd" value="<%=request.getParameter("password")%>"/> </body> </html>效果截圖如下:
點擊按鈕后:
總結
以上是生活随笔為你收集整理的js打开新窗口并且POST传入参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SVN cleanup 反复失败解决办法
- 下一篇: Java微信公众平台获取签名