2017-03-02 17:38
浏览 74

如何在PHP代码中调用jquery函数 - $未定义

I want to use jQuery-confirm dialog instead of echoing a Javascript alert from PHP. When I run following code it shows me this error:

$ is not defined

echo "<script>$.alert({title: 'Alert!',content: 'Simple alert!',});</script>";
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • duanbipu7601 2017-03-02 17:48

    Be sure to include the jQuery library first.

    Then, if it still do not work, like you say in comments, try wrapping your alert inside a $(document).ready():

    echo "<script>$(document).ready(function(){$.alert({title: 'Alert!',content: 'Simple alert!'});});</script>";
    点赞 评论
  • dongyupan4850 2017-03-02 17:41

    You must understand that Javascript runs on the client side (the browser) try outputting this to a html page while including the script source for Jquery-confirm and you'll see that $.alert started working.

    点赞 评论
  • douling8772 2017-03-02 17:42

    you want confirm that jQuery library is linked or not... or You can place your code after the jQuery library included...

    点赞 评论
  • douming4359 2017-03-03 07:10

    Endless thanks to all. I have solved my problem with moving script libraries to my header section. And it works now ;)

    点赞 评论

相关推荐 更多相似问题