layerUI子页面和父页面交互,实现动态添加参数到父页面。前端代码如下:
父页面index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css" src="/css/layui.css"></style>
<script type="text/javascript" src="/js/common/jquery-3.4.1.js"></script>
<script type="text/javascript" src="/js/common/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="/js/common/layer.js"></script>
</head>
<body>
<div>
<div id="box">
<div><input type="text" id="parentIframe" value="parentIframe"></div>
</div>
<table border="1" id="userTable" calss="layui-table">
<thead>
<tr>
<th>姓名</th>
<th>城市</th>
<th>爱好</th>
</tr>
</thead>
<tbody>
<tr>
<td class="nameClass"><input type="text" value="小美" name="nameClass"></td>
<td class="cityClass"><input type="text" value="北京" id="cityClass"></td>
<td class="likeClass" labelTooltip="羽毛球 | 乒乓球" ><input type="text" value="运动" class="likeClass"></td>
</tr>
<tr>
<td class="nameClass"><input type="text" value="小米" name="nameClass"></td>
<td class="cityClass"><input type="text" value="上海" ></td>
<td class="likeClass" labelTooltip="钢琴 | 二胡">
<div id="idDiv">
<input type="text" id="sy" value="声乐"/>
</div>
</td>
</tr>
</tbody>
</table>
<input type="button" id="test2" name="test2" value="弹出"></input>
</div>
</body>
<script type="text/javascript" src="/js/index/index.js"></script>
</html>
index.js
$("#submitAll").click(function (){
//注意:parent 是 JS 自带的全局对象,可用于操作父页面
var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
var tabVal = new Array();
$("#idUserTable tbody").find("tr").each(function(){
var id = $(this).find("input[name='nameClass']").val();
var val = $(this).find("input[name='likeClass']").val();
var backStr='<input type="text" id="'+id+' "value="'+val+'"/>';
parent.$('#parentIframe').val('我被改变了');
parent.layer.tips('Look here', '#parentIframe', {time: 5000});
parent.$('#idDiv').append(backStr);
console.log("backStr:"+backStr);
tabVal.push({
name:$(this).find("input[name='nameClass']").val(),
city:$(this).find("input[name='cityClass']").val(),
like:$(this).find("input[name='likeClass']").val()
})
});
console.log(tabVal);
parent.layer.close(index);
});
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/js/common/jquery-3.4.1.js"></script>
<script type="text/javascript" src="/js/common/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="/js/common/layer.js"></script>
</head>
<body>
<div>
<table border="1" id="idUserTable">
<thead>
<tr>
<th>姓名</th>
<th>城市</th>
<th>爱好</th>
</tr>
</thead>
<tbody>
<tr>
<td class="nameClass"><input type="text" value="小明" name="nameClass"></td>
<td class="cityClass"><input type="text" value="成都" name="cityClass"></td>
<td class="likeClass"><input type="text" value="运动" name="likeClass"></td>
</tr>
<tr>
<td class="nameClass"><input type="text" value="小米" name="nameClass"></td>
<td class="cityClass"><input type="text" value="上海" name="cityClass"></td>
<td class="likeClass"><input type="text" value="声乐" name="likeClass"></td>
</tr>
</tbody>
</table>
<input type="button" id="submitAll" name="submitAll" value="确认提交"></input>
</div>
</body>
<script type="text/javascript" src="/js/index/index.js"></script>
</html>
后台:
package com.example.demo.contorller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "index";
}
@RequestMapping("/test.do")
public String test() {
return "test";
}
}