layer父窗口调用子窗口函数例子

子窗口html,一个弹出框,里面有各种form选项:

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    
    <title>fixed display number 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 href="<%=path%>/res/main.css" rel="stylesheet" type="text/css" />
	<script src="<%=path%>/res/js/jquery-1.11.3.min.js"></script>
	<script type="text/javascript" src="<%=path %>/res/js/jquery.form.js"></script>
	<script type="text/javascript" src="<%=path%>/res/js/ueditor/ueditor.all.js"></script>
	<script type="text/javascript" src="<%=path%>/res/js/ueditor/ueditor.config.js"></script>
	<link rel='stylesheet' type='text/css' href="<%=path%>/res/js/ueditor/themes/default/css/ueditor.css"/>
	<script type="text/javascript" src="<%=path%>/res/js/ueditor/lang/zh-cn/zh-cn.js"></script>
 <style>
 #fixed-display-number-page-form{
 	margin-left:5%;
 	margin-top:5%;
 }
 </style>
  </head>
  <body>
  <span id="fixed-display-number-page-form-radio" style="visibility:hidden">${batch.displayNumType}</span>
  <span id="fixed-display-number-page-batchId" style="visibility:hidden">${batch.batchId}</span>
  <form  id="fixed-display-number-page-form">
  <input type="radio" name="displayNumber" id="displayNumberRadio" checked="checked"  value="1" />固定外显
  <select id="fixed-display-number-page-form-radio-select1">
  <c:forEach var="obj" items="${fixedDisplayNumberList}">
  <option <c:if test="${batch.displayNum==obj.fixedDisplayNumber}">selected="selected"</c:if> value="${obj.fixedDisplayNumber}">${obj.fixedDisplayNumber}</option>
  </c:forEach>
  </select>
  <br/>
   <input type="radio" name="displayNumber" id="displayNumberRadio" value="2" />随机外显
   <br/>
   <input type="radio" name="displayNumber" id="displayNumberRadio" value="3" />本网固定外显+他网随机外显
   <select id="fixed-display-number-page-form-radio-select2">
  <c:forEach var="obj" items="${fixedDisplayNumberList}">
  <option <c:if test="${batch.displayNum==obj.fixedDisplayNumber}">selected="selected"</c:if> value="${obj.fixedDisplayNumber}">${obj.fixedDisplayNumber}</option>
  </c:forEach>
  </select>
  </form>
   <script>
  $(document).ready(function(){
	  //取span的值
	  var radioValue = $("#fixed-display-number-page-form-radio").html();
	  $("input[name='displayNumber'][value='"+radioValue+"'").attr("checked",true);
	  
	  $.fn.saveFixedDisplayNumber=function(){
	      //保存数据
		 var displayNumType=$("input[name='displayNumber']:checked").val();
	    // var displayNum=$("#fixed-display-number-page-form-radio-select option[selected]").val(); 
	     displayNum=$("#fixed-display-number-page-form-radio-select option:selected").val();
	     if(displayNumType==1){
		    displayNum=document.getElementById("fixed-display-number-page-form-radio-select1").value;
	     }else if(displayNumType==3){
	     	displayNum=document.getElementById("fixed-display-number-page-form-radio-select2").value;   
	     }else{
	    	 displayNum=null; 
	     }
	     let batchId=$("#fixed-display-number-page-batchId").html();
	     //请求数据给后台
	     var url="<%=path%>/batch/saveDisplayNumber";
	     postData(displayNum,displayNumType,batchId,url);
	    }
	  function postData(displayNum,displayNumType,batchId,url){
		  $.ajax({ 
  		    type:"POST", 
  	        url:url,  
  	        cache:false, 
  	        data:{
  	        	"displayNum":displayNum,
  	        	"displayNumType":displayNumType,
  	        	"batchId":batchId
  	        	}, 
  	        async:false,
  	        success:function(data){
  	        	data=JSON.parse(data);
  	        	var code=data.code;
  	        	var errmsg=data.errmsg;
  	        	if(code==0){
  	        		alert("保存成功");
  	        	}else{
  	        		alert(errmsg);
  	        	}
  	        }, 
  	        error:function(jqXHR, textStatus, errorThrown){
  	        	alert("请求失败");
  	        },
  	        complete:function(){
  	        	
  	        }
  	    }); 
	  }
  });
  function saveFixedDisplayNumber(){
	  $().saveFixedDisplayNumber();
	}
  
  </script>
  </body>
  </html>

父窗口调用实例:

 

 function displayNumberSetting(id){
        	layer.open({
        		id:id,
        		//skin: 'layui-layer-molv', //样式类名
        		type: 2,
        		title: '外显配置',
        		shadeClose: true,
        		shade: false,
        		maxmin: true, //开启最大化最小化按钮
        		btn:['保存','关闭'],
        		area: ['435px', '300px'],
        		content: '<%=path%>/batch/fixedDisplayNumberPage?batchId='+id,
        		yes: function(index,layero) {
        			var body = layer.getChildFrame('body', index);
        		    var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象
        		    //执行iframe页的方法
        		    //iframeWin.method();
        		    //得到iframe页的body内容
        		    //console.log("Iframe内容:"+body.html());
        		    //调用iframe的方法
        		    iframeWin.saveFixedDisplayNumber();
        		    layer.close(index); //关闭弹窗
        			//保存form的用户数据
        		},
        		cancel: function(){ 
        			//do nothing
        		}
        	});
        }

  

posted @ 2020-06-18 17:17  Lost blog  阅读(923)  评论(0编辑  收藏  举报