dongxiang3205 2015-03-06 10:35
浏览 50
已采纳

PHP:从case语句回应JQuery Alert

I am trying to echo a JQuery alert from PHP. The method I am using is to echo the entire script tags and then the JQuery within it. JQuery is working because alerts elsewhere work. the JQuery library is loaded before the code i am trying so it is not that.

below is my current statement:

date_default_timezone_set('Europe/London'); #server timezone
    $now = date('His'); #current time
    $cl = '173000'; #closing time
    $op = '084500'; #opening time

    switch (true) {
        case $now < $cl && $now > $op:
            echo "open
            <script type='text/javascript'>
                $(document).ready(function(){
                    alert('open');
                });
            </script>
            ";
            break;

        case $now > $cl && $now < $op:
            echo 'closed';
            break;

        default:
            echo("neither closed nor open.  dun dun duuuuun");
            break;
    }

I know the statement is working to some extent because the open string is displayed. I also see the script tags yet they don't seem to execute see below:


This image shows that the open text just before the script is echo'd into the page

The script is wrote into the page but not terminated


This image shows that the script tag is put into the page but is not executed

This is where the open text works


Update:

The answer was that JQuery hadn't actually loaded like i thought it had. the version number was wrong. for some reason it was:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.4.3/jquery.min.js"></script>

I have now changed it to:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2/jquery.min.js"></script>

the reason I left the 2.x.x out is because the hosted library will choose the most up to date version of 2 now.

  • 写回答

2条回答 默认 最新

  • dongmanzui8486 2015-03-06 10:41
    关注

    Are you sure you have jQuery loaded at that moment you want to alert? By the way, If you want to just alert something, you don't really need jQuery, just use:

    <script>alert('open');</script>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝