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条)

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题