bootstrap的modal-remote两种加载方式

方法一:

使用链接
<a href="demo.jsp" data-toggle="modal" data-target="#mymodal">打开</a>
当点击该连接时,demo.jsp的内容就可以动态的加载到<div class="modal-content"></div>中。当然这里的连接也可以是controller

方法二:

使用脚本

$("#myModal").modal({  
    remote: "page.html"  
});  


但是这样加载后,会有问题,modal数据只加载一次,如果要加载不同的数据,例如根据id查询详细信息,modal的数据是不能更新的,即使传的id值不同。其实解决办法很简单,只需要在加载下次数据前,将之前的加载的数据清除即可。
最简单的方式就是监听modal的hidden,当modal关闭时,即把数据清除即可:

//v2
$("#myModal").on("hidden", function() {  
    $(this).removeData("modal");  
});  
//v3
$("#myModal").on("hidden.bs.modal", function() {  
    $(this).removeData("bs.modal");
}); 


问题来了:如果在请求的页面中有$()加载事件加载比如boostrap-validator或者boostrap-fileinput等插件会出现奇怪的现象,第一次正常执行,关掉modal,第二次,$()的代码没有执行,第三次能执行;经过反复发现“hidden.bs.modal”监听每次都执行了,但是加载到<div class="modal-content"></div>里面的数据没有被清除,可能是这个原因导致的这种现象,于是改成如下代码:

$("#myModal").on("hidden.bs.modal", function() {  
    $(this).removeData("bs.modal");
    /*modal页面加载$()错误,由于移除缓存时加载到<div class="modal-content"></div>未移除的数据,手动移除加载的内容*/
    $(this).find(".modal-content").children().remove(); 
}); 

我现在就是这样写的确实有效!

在page.html页面不要引用任的js或样式文件,所需的都在主页面引用即可,因为加载后它就属于主页面,并且能读取主页面里的参数啥的
转载原文:https://blog.csdn.net/kewanjun_lcx/article/details/54748286 

 

在主页面里写的代码

 

// 加载一个html页面到Madal框里 二次下单
	function GetSecondOrderAddDivModal(){
	    $("#SecondOrderAddDiv").modal({
	        remote: "views/order/orderSecondAddNew.html"
	        });
	}
// 清除重要参数数据 防止加载数据不变
$("#SecondOrderAddDiv").on("hidden.bs.modal", function() { 
	 $(this).removeData("bs.modal"); 
	  $(this).find(".modal-content").children().remove();
});

<div class="modal fade bs-example-modal-lg" id="SecondOrderAddDiv" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true" data-backdrop="static"> 
        <div class="modal-dialog modal-lg"> 
             <input type="hidden" id="secondsourceflag" value="">
            <div class="modal-content">
            </div> 
        </div> 
    </div>

在page.html写的

<style>
.testarealeft{
	height: 50px;
	width:270px;
	border:0;
	resize:none
}
.testarearight{
	height: 40px;
	border:0;
	resize:none
}
.file-preview-frame{
   height:110px;
}
.file-preview{
   width: 500px;
}
</style>
<div class="modal-header"> 
    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 
    <h4 class="modal-title"></h4> 
</div>
<div class="modal-body">
	<div class="wrapper wrapper-content animated fadeInRight ecommerce">
		<div class="ibox-content m-b-sm border-bottom">
	        <div class="row">
	            <div style="text-align:center;">
		            <div><label style="font-size: 35px;font-family: cursive;"></label></div>
		            <div style="padding-top: 30px;">
                        这里都可以写自己需要的代码
			        </div>    
		       </div>
	        </div>
	    </div>
	</div>
</div>	
<script type="text/javascript">
$(function(){
	这里都可以写自己需要的代码
});
// 刷新页面
function autorefresh(){
	window.location.reload();
}

</script>
			     

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值