頁面傳值是很是經常使用的,layui自帶彈窗功能,可是內置使用的是location.href 暫時沒找到方法條件請求頭,因此在後臺須要放開攔截器,html
layer.open({ type : 2, title: "分類詳情", area: ["50%","90%"], closeBtn: false, shadeClose: true, //點擊遮罩關閉 btn: ['保存', '關閉'] , yes: function(index,layero){ var winds = $(layero).find("iframe")[0].contentWindow; var itemCatDetail ={ cateDeId:winds.cateDeId, cateId:data.catId, introduction:winds.layedit.getContent(winds.editor), imgStr:winds.imgUrlList, videoStr:winds.videoUrl } console.log(JSON.stringify(itemCatDetail)); if(!lock) { lock=true; layui.$.ajax({ type: "POST", url: "/itemCat/addItemCatDetail", data: JSON.stringify(itemCatDetail), dataType: 'json', contentType: "application/json; charset=utf-8", beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("token", layui.data(layui.setter.tableName)["token"]); }, success: function (res) { layer.msg(res.msg); var iframeWin = window[layero.find('iframe')[0]['name']]; iframeWin.location.reload; }, complete: function( xhr,data ){ layui.data(layui.setter.tableName, { key: "token", value: xhr.getResponseHeader("token") }) } }); } }, btn2: function(){ layer.closeAll(); }, shade: 0, content : "CateDetail.html", success:function(layero,index){ var body = layer.getChildFrame('body', index); body.contents().find("#cateId").val(data.catId); }}); }
在layer.open的success方法中拿到子頁面的body對象 var body = layer.getChildFrame('body', index);
而後對隱藏的<input type="hidden" name="parentId" id="cateId">中賦值 body.contents().find("#cateId").val(data.catId);此時直接獲取便可
而後能夠使用 var winds = $(layero).find("iframe")[0].contentWindow;獲取子頁面的window對象能夠拿到子頁面定義的變量的值,便可獲取子頁面的值,而後請求後臺便可
子頁面也能夠使用
$("#back").click(function () {
var indexNow = parent.layer.getFrameIndex(window.name); //先獲得當前iframe層的索引
parent.location.reload();
parent.layer.close(indexNow);
})關閉子頁面,並刷新父頁面