【问题标题】:Javascript ajax if then elseJavascript ajax if then else
【发布时间】:2014-05-21 11:11:05
【问题描述】:

如果下面的 php 脚本返回高于 0 的 mysql 选择计数结果,我有以下 javascript 应该向用户弹出通知:

<script>
        $(document).ready(function() {
            function update() {
                $.ajax({
                    type: 'POST',
                    url: '../ajax/alert/notification.php',
                    timeout: 1000,
                    success: function(data) {
                        $("#notification-alert-main").text(data);
                        $("#notification-alert-detailed").text(data);
                        if (data > 0) {
                            $("#notification-alert-message").innerHTML('<i class="fa fa-warning danger"></i> New alert has been detected');
                        }
                        window.setTimeout(update, 5000);
                    },
                });
            }
            update();
        });
    </script>

php 脚本:

<?php
  include('../../db.php');
  $queryCountUnreadAlerts = $bdd->prepare("SELECT COUNT(*) Nb FROM exp_alert WHERE ale_read = 0;");
  $queryCountUnreadAlerts->execute();
  $dataCountUnreadAlerts = $queryCountUnreadAlerts->fetch();
  $queryCountUnreadAlerts->closeCursor();
  echo(intval($dataCountUnreadAlerts['Nb']));
?>

网络浏览器向我报告错误:

[Error] TypeError: 'undefined' is not a function (evaluating '$("#notification-alert-message").innerHTML('<i class="fa fa-warning danger"></i> New alert has been detected')') success (index.php, line 230) l (jquery.min.js, line 4) fireWith (jquery.min.js, line 4) k (jquery.min.js, line 6) (anonymous function) (jquery.min.js, line 6)

我怀疑变量类型有误,但我仍然一直在使用整数。

【问题讨论】:

  • innerHTML是DOM节点属性,不是jQuery方法,使用.html('...')

标签: javascript php jquery mysql ajax


【解决方案1】:

使用.html() 而不是innerHTML

或者

如下修改

$("#notification-alert-message").get(0).innerHTML="&lt;i class='fa fa-warning danger'&gt;&lt;/i&gt; New alert has been detected";

【讨论】:

  • 显示您从帖子中得到的回复.. data中的值
【解决方案2】:

在 JQuery 中,您使用 .html(),而不是 .innerHTML

http://api.jquery.com/html/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多