需求:
通过跳转窗口,将必要信息回写到主页面中(必要字段为只读)
目前此示例模型不含后端数据传输,只有前端三个jsp页面
实现步骤:
- 主页面跳转到第二个页面
- 第二个页面通过查询条件,跳转到第三个页面
- 第三个页面中显示所有查询结果,双击某条数据,将内容回填到第二个页面
- 第二个页面会显示此条数据所有信息,点击保存回填到主页面
知识点:
打开新窗口
window.open(URL,name,specs,replace)
获取页面中id为UnitName的值
document.getElementById(“UnitName”).value;
获取页面中第0个name为customerName的值
document.getElementsByName(“customerName”)[0].value;
返回打开当前窗口的窗口对象
window.opener
mainPage.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>主页</title>
</head>
<body style="text-align: center;">
<tr>
<td align=center>
<input type="hidden" id="address" name="address" value="">
</td>
</tr>
<input type="button" name="payeeInfo" class="button" value="组织机构信息" onClick="openSubquery();"/>
<table align="center">
<tr>
<td class=title>组织机构名称:</td>
<td class=input><input type="text" class="common" readonly="readonly"
name="customerName"
maxlength="20"></td>
<tr>
<td class=title>注册资本金:</td>
<td class=input><input type="text" class="common"
name="registeredFunds"
id="registeredFunds"
maxlength="80"
readonly="readonly"><br/></td>
</tr>
<tr>
<td class=title>备注:</td>
<td class=input><input type="text" class="common"
id="remarks"
maxlength="80"></td>
</tr>
<tr>
<td colspan="4"><input type="button" value="保存到数据库"
onClick="return saveForm()" class="button" style="cursor: hand;float: right"></td>
</tr>
</table>
</body>
</html>
<script language="javascript">
function openSubquery() {
var url = "/untitled1_war_exploded/window/subpage.jsp";
window.open(url, "客户信息查询", "top=100,left=100,status=no,toolbar=no,menubar=no,resizable=1,location=0,directories=0,scrollbars=yes,width=800,Height=600");
}
function saveForm() {
var address = document.getElementById("address").value;
var customerName = document.getElementsByName("customerName")[0].value;
var registeredFunds = document.getElementById("registeredFunds").value;
var remarks = document.getElementById("remarks").value;
if (trim(customerName) === "") {
alert("请选择组织机构名称!此项为必录项,请检查!");
return false;
}
address = "注册地为:" + address;
alert(address)
}
</script>
<script src="${pageContext.request.contextPath}/js/common.js"></script>
subpage.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>子页</title>
</head>
<body>
<form action="" method="post" name="fm">
<div id="divName">
<table cellpadding="3" cellspacing="1" class="common">
<tr>
<td class=listtitle colspan="4">组织机构基本信息</td>
</tr>
<tr>
<td class=title>机构名称</td>
<td class=input>
<input type="text" class="common" name="customerName" id="customerName">
</td>
<input type="hidden" class="common" name="industry" id="industry">
<td class=title>行业名称</td>
<td class=input>
<input type="text" class="codename" name="industryName" value="">
</td>
</tr>
<tr>
<td class=title>住所(注册地)</td>
<td class=input>
<input type="text" class="common" name="address" id="address">
</td>
<td class=title>经营范围</td>
<td class=input>
<input type="text" class="common" name="businessScope" id="businessScope">
</td>
</tr>
<tr>
<td class=title>注册资本金</td>
<td class=input>
<input type="text" class="common" name="registeredFunds" id="registeredFunds">
</td>
<td class=title>注册资本金币种</td>
<td class=input>
<input type="text" class="codename" name="registeredFundsType" id="registeredFundsType" value="">
</td>
</tr>
</table>
<table class=common>
<tr align="center">
<td>
<input type="button" class="button" value="保 存" id="saveButton" name="saveButton"
onclick="return formToSave();">
</td>
<td>
<input type="button" class=button name="return" size="2" value="返 回"
onclick="window.opener=null;window.close();">
</td>
</tr>
</table>
</div>
</form>
<hr>
<form action="" name="queryForm">
<table>
<tr>
<td class=listtitle colspan="4">组织机构信息查询</td>
</tr>
<tr></tr>
<tr align="left">
<td class=title>机构名称</td>
<td class=input>
<label for="UnitName"></label><input type="text" id="UnitName">
</td>
<td class=title>机构代码</td>
<td class=input>
<input type="text" id="UnitCard">
</td>
</tr>
<tr align="center">
<td colspan=4 class=button align="center">
<input class="button" type="button" name="query" alt=" 查 询 " value="查 询"
onclick="return submitQueryForUnit()">
</td>
</tr>
</table>
</form>
</body>
</html>
<script language="javascript">
function formToSave() {
var data1 = document.fm.customerName.value;
var data2 = document.fm.registeredFunds.value;
var address = document.fm.address.value;
console.log(data1);
console.log(data2);
console.log(address);
window.opener.document.getElementsByName("customerName")[0].value = data1;
window.opener.document.getElementsByName("registeredFunds")[0].value = data2;
window.opener.document.getElementById("address").value = address;
window.close()
}
function submitQueryForUnit() {
var name = document.getElementById("UnitName").value;
var card = document.getElementById("UnitCard").value;
var strURL = "/untitled1_war_exploded/window/third.jsp?&name=" + name + "&card=" + card;
if (trim(name) === "" && trim(card) === "") {
alert("请选择机构名称或机构代码!至少有1项为必录项,请检查!");
return false;
}
window.open(strURL, "信息子查询", "top=40,left=100,status=no,toolbar=no,menubar=no,resizable=1,location=0,directories=0,scrollbars=yes,width=1400,Height=700");
}
</script>
<script src="${pageContext.request.contextPath}/js/common.js" charset="UTF-8"></script>
third.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>第三页</title>
</head>
<body>
<form name=fm action="" method=post>
<table id="commonTable" class=common cellpadding="3" cellspacing="1">
<tr>
<td class=centertitle width='6%'>名称</td>
<td class=centertitle width='6%'>行业名称</td>
<td class=centertitle width='6%'>地址</td>
<td class=centertitle width='6%'>经营范围</td>
<td class=centertitle width='6%'>注册资本金</td>
<td class=centertitle width='6%'>币种</td>
</tr>
<%
for (int i = 0; i < 10; i++) {
%>
<tr name="a" class="classes"
onmouseover="stay(this,'over')"
onmouseout="stay(this,'out')"
onclick="stay(this,'on')"
ondblclick="backfillsuper(this);">
<td><%="名称"+(1+i)%>
<td><%="参数2"%>
<td><%="参数3"%>
<td><%="参数4"%>
<td><%="参数5"%>
<td><%="参数6"%>
</tr>
<%
}
%>
</table>
</form>
</body>
</html>
<script language="javascript">
function stay(thisRow, flag) {
if (flag === 'over') {
if (thisRow.style.backgroundColor !== '#cfff00') {
thisRow.style.backgroundColor = '#cfffce';
}
} else if (flag === 'out') {
if (thisRow.style.backgroundColor !== '#cfff00') {
thisRow.style.backgroundColor = '';
}
} else {
thisRow.style.backgroundColor = '#cfff00';
}
return false;
}
function backfillsuper(field) {
window.opener.document.getElementsByName("customerName")[0].value = field.getElementsByTagName("td")[0].innerHTML.trim();
window.opener.document.getElementsByName("industryName")[0].value = field.getElementsByTagName("td")[1].innerHTML.trim();
window.opener.document.getElementById("address").value = field.getElementsByTagName("td")[2].innerHTML.trim();
window.opener.document.getElementById("businessScope").value = field.getElementsByTagName("td")[3].innerHTML.trim();
window.opener.document.getElementById("registeredFunds").value = field.getElementsByTagName("td")[4].innerHTML.trim();
window.opener.document.getElementById("registeredFundsType").value = field.getElementsByTagName("td")[5].innerHTML.trim();
window.close();
}
</script>
common.js
/**
* 去除字符串两端空白
* @param value
* @returns {*}
*/
function trim(value) {
return leftTrim(rightTrim(value));
}
function leftTrim(value) {
var re = /^\s*/;
if (value == null) {
return null;
}
return value.replace(re, "");
}
function rightTrim(value) {
var re = /\s*$/;
if (value == null) {
return null;
}
return value.replace(re, "");
}
/**
* 选择弹窗
*/
function show_confirm() {
var result = confirm('是否删除!');
if (result) {
alert('删除成功!');
} else {
alert('不删除!');
}
}
/**
* 通过输入框判断
*/
function show_prompt() {
var value = prompt('请输入姓名:', '默认名字');
if (value == null) {
alert('你取消了输入!');
} else if (value === '') {
alert('姓名输入为空,请重新输入!');
show_prompt();
} else {
alert('你好,' + value);
}
}
/**
* 将多个输入框覆盖到当前页面
*/
function disp_prompt() {
for (var i = 0; i < 5; i++) {
var msg = prompt("请依次输入5个参数");
var msgArray = msg.split(" ");
document.write(msgArray + "</br>");
}
}