可能很多人都和我遇到相同的问题:
一般解决这个问题的办法就是,在php代码中向前台echo一段js代码,如:
echo '<script>alert("提示消息!");</script>';
这样echo出来前台是正常显示的,但是用layer.msg();就会没有提示,解决方法如下:
首先要echo layer.msg();的页面必须引入jquery.js及layer.js
所以我们可以直接在后台这样写
echo '<script type="text/javascript" src="/Public/js/jquery.js"></script>';
echo '<script type="text/javascript" src="/Public/js/layer/layer.js"></script>';
然后
echo '<script>$(document).ready(function(){layer.msg("'.$message.'");});</script>'; //$message 是你要提示的内容 这里我做了一个变量.
解释一下 后台使用layer.msg();最外层一定要用ready函数,否则js库资源文件引入的延迟就会导致layer.msg();执行失败;undefined;
为了方便我直接在我项目的common.php封装了success和error
protected function leError($message, $jumpUrl = '', $time = 2000){
echo '<script type="text/javascript" src="/Public/js/jquery.js"></script>';
echo '<script type="text/javascript" src="/Public/js/layer/layer.js"></script>';
echo '<script>$(document).ready(function(){layer.msg("'.$message.'");setTimeout(function(){window.location.href="'.$jumpUrl.'";},'.$time.')});</script>';
}
protected function leSuccess($message, $jumpUrl = '', $time = 2000) {
echo '<script type="text/javascript" src="/Public/js/jquery.js"></script>';
echo '<script type="text/javascript" src="/Public/js/layer/layer.js"></script>';
echo '<script>$(document).ready(function(){layer.msg("'.$message.'");setTimeout(function(){window.location.href="'.$jumpUrl.'";},'.$time.')});</script>';
}
调用的时候可以继承这个函数所在的类
$this->leError('提示信息',U('Index/index'));
如果不想让它跳转,而是直接返回上一级代码可以改成这样的:
protected function leError($message, $jumpUrl = '', $time = 2000){
echo '<script type="text/javascript" src="/Public/js/jquery.js"></script>';
echo '<script type="text/javascript" src="/Public/js/layer/layer.js"></script>';
if($jumpUrl != ''){
echo '<script>$(document).ready(function(){layer.msg("'.$message.'");setTimeout(function(){window.location.href="'.$jumpUrl.'";},'.$time.')});</script>';
}else{
echo '<script>$(document).ready(function(){layer.msg("'.$message.'");setTimeout(function(){window.history.back(-1);},'.$time.')});</script>';
}
}
这样就方便在其它代码处 直接调用;
希望对你有用!