layer.open中父頁面向子頁面傳值


1.咱先看圖說話

父list.jsp 子operate.jsp
實現的代碼
1 在父頁面上完成對子頁面的數據渲染
function setData(data) {
var lay=layer.open({
type: 2,
title: false,
//closeBtn: 0,
//shade:0,//是否有遮罩效果
area: ['560px', '294px'],//寬,高
//skin: 'layui-layer-nobg', //沒有背景色
shadeClose: false,
content: ['operate.do', 'no'],
success: function(layero, index){
var body=layer.getChildFrame('body',index);//少了這個是不能從父頁面向子頁面傳值的
        //獲取子頁面的元素,進行數據渲染
       body.contents().find("#attributeNameEn").val(data.attributeNameEn);
       body.contents().find('#attributeNameCn').val(data.attributeNameCn);
       body.contents().find('#attributeId').val(data.attributeId);
       body.contents().find('#remark').val(data.remark);
       body.contents().find('#remarkEn').val(data.remarkEn);
       let id = '#isValid' + data.isValid;
       body.contents().find(id).prop("checked", true);//這是子頁面的單選按鈕,讓其選中
 },
});
//重新給指定層設定width、top等 因為它們的小屏幕電腦的彈出層會出現滾動條,所以才用這個方法給加一個overflow: 'visible',屬性
layer.style(lay, {
overflow: 'visible',
});

}

2.保存按鈕的事件寫在子頁面的
function saveByajax() {
$.ajax2({
type: 'POST',
url: 'xxxx',
data:{
attributeId:$('#attributeId').val(),
attributeNameEn:$('#attributeNameEn').val(),
isValid:$('input[name="radioInline"]:checked').val(),
},
success: function (r) {
if (r && r.message=='Operate successfully') {
// layer.msg("操作成功!");
setTimeout( function(){
//window.parent.location.href = 'list.do'; 或者
parent.location.reload();//刷新父頁面
}, 5 * 100 );//延遲500毫秒
//window.parent.layer.closeAll();關閉彈窗
}else {
// layer.msg("添加失敗!");
layer.msg("<spring:message code="msg_Add_failed"/> !");
}
}
})
}
注意:收到小伙伴的私信說,上面例子從父頁面不能把值傳到子頁面去,比如下拉框未選中,單選按鈕也未選中
原因:body.contents()這個不能少,還有就是body.contents().find("#XXXX"),這個 xxx 是你子頁面的元素的id,不是name


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM