gpt4 book ai didi

Javascript函数自身调用

转载 作者:行者123 更新时间:2023-12-03 11:12:50 25 4
gpt4 key购买 nike

我想在单击 a 时显示自定义警报,但如果我登录,我想重定向到另一个页面。我有这个。

        <div id="contentCalendar" class="col-md-3 images_1_of_4 text-center">
<a><img onClick="show_alert();" id="rotateme" class="img-responsive img-circle center-block" src="web/images/calendaricon.png"/></a>
<h4><a href="#">Check our last events</a></h4>

</div>
<div id="myalert" class="alert alert-error">
<a href="#" class="close" data-dismiss="alert">&times;</a>
<strong>Error!</strong> You must be logged in to see our calendar.
</div>
<script>
$('#myalert').hide();
//What happen if you want to enter the events without loggin.
var logged_in = <?php echo ($logged_in); ?>;
function show_alert()
{
if(logged_in==true)
{
window.location="timeline.php";
}
else
{
$('#myalert').show();
}
}
</script>

出于某种原因,当 Logged_in 为 true 并且我被重定向时,它就像一个魅力。但是当我注销时,即使我根本没有按下按钮,它也不会显示警报。

有什么想法吗?

最佳答案

永远,再次,永远,通过 json_encode 运行您嵌入 JS 的所有内容:

var logged_in = <?php echo json_encode($logged_in); ?>;

当你简单地在 php 中回显一个虚假值时,它不会回显任何内容,从而破坏你的 JavaScript。

关于Javascript函数自身调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27487602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com