javascript - 除非刷新页面,否则 jQuery Ajax 点击不起作用

标签 javascript php jquery ajax

我一直在我的 PHP 项目中使用一些 JS 和 ajax,我遇到了一些问题。 ajax 似乎仅在单击后刷新页面或仅刷新以单击时才起作用。我正在使用 jquery 3.2.1。

我最初使用的是 .live(),但现在已弃用,我读到 .on() 是替代方案,所以我现在正在使用它。

这是我的 JS 部分:

  $(".up_vote").on("click", function() {
    var post_id = $(this).attr('postid');
    $.ajax({
      type: "POST",
      data: {
        'post_id': post_id,
        'action': 'upvote'
      },
      url: '<?php echo $url; ?>/includes/functions.php',
      dataType: 'json',
      success: function(response) {
        if (response.ResponseCode == 200) {
          $('#postbox_' + post_id).load('<?php echo $url; ?>/index.php #postbox_' + post_id + ' >*');
        } else {
          alert(response.Message);
        }
      }
    });
  });

  $(".down_vote").on("click", function() {
    var post_id = $(this).attr('postid');
    $.ajax({
      type: "POST",
      data: {
        'post_id': post_id,
        'action': 'downvote'
      },
      url: '<?php echo $url; ?>/includes/functions.php',
      dataType: 'json',
      success: function(response) {
        if (response.ResponseCode == 200) {
          $('#postbox_' + post_id).load('<?php echo $url; ?>/index.php #postbox_' + post_id + ' >*');
        } else {
          alert(response.Message);
        }
      }
    });
  });

  $("#btnpost").click(function() {
    var post = $("#post_feed").val();
    if (post == "") {
      alert("This can't be empty.");
      return false;
    } else {
      $.ajax({
        type: "POST",
        data: {
          'post_feed': post,
          'action': 'post'
        },
        url: '<?php echo $url; ?>/includes/functions.php',
        dataType: 'json',
        success: function(response) {
          if (response.ResponseCode == 200) {
            $("#post_feed").val("");
            $('#feed_div').load('<?php echo $url; ?>/index.php #feed_div');
          } else {
            alert(response.Message);
          }
        }
      });
    }
  });
});

我是否遗漏或做错了什么?

最佳答案

首先确保您击中了目标,试试这个,看看警报是否显示:

$(function() {
    $(document).on('click', '.up_vote', function() {
       alert('ok');
    });
});

点击事件附加到页面加载时的现有元素。这里的关键词是“现有”。当我们用 ajax 加载一些东西时,我们操纵了 DOM。我们正在放置全新的元素。所以我们需要做的是在放置新内容后附加该事件。

所以为了在每次调用 ajax 时附加事件,我们使用 .on

相应地编辑你的代码

  $(document).on('click', '.up_vote', function() {
    var post_id = $(this).attr('postid');
    $.ajax({
      type: "POST",
      data: {
        'post_id': post_id,
        'action': 'upvote'
      },
      url: '<?php echo $url; ?>/includes/functions.php',
      dataType: 'json',
      success: function(response) {
        if (response.ResponseCode == 200) {
          $('#postbox_' + post_id).load('<?php echo $url; ?>/index.php #postbox_' + post_id + ' >*');
        } else {
          alert(response.Message);
        }
      }
    });
  });

  $(document).on('click', '.down_vote', function() {
    var post_id = $(this).attr('postid');
    $.ajax({
      type: "POST",
      data: {
        'post_id': post_id,
        'action': 'downvote'
      },
      url: '<?php echo $url; ?>/includes/functions.php',
      dataType: 'json',
      success: function(response) {
        if (response.ResponseCode == 200) {
          $('#postbox_' + post_id).load('<?php echo $url; ?>/index.php #postbox_' + post_id + ' >*');
        } else {
          alert(response.Message);
        }
      }
    });
  });

  $(document).on('click', '#btnpost', function() {
    var post = $("#post_feed").val();
    if (post == "") {
      alert("This can't be empty.");
      return false;
    } else {
      $.ajax({
        type: "POST",
        data: {
          'post_feed': post,
          'action': 'post'
        },
        url: '<?php echo $url; ?>/includes/functions.php',
        dataType: 'json',
        success: function(response) {
          if (response.ResponseCode == 200) {
            $("#post_feed").val("");
            $('#feed_div').load('<?php echo $url; ?>/index.php #feed_div');
          } else {
            alert(response.Message);
          }
        }
      });
    }
  });
});

关于javascript - 除非刷新页面,否则 jQuery Ajax 点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43169394/

相关文章:

javascript - jquery grep,使用正则表达式过滤

javascript - 在文本输入中 react native 用户标签

javascript - CSS/HTML 中的弹出消息

php - ajax中的jqGrid本地数据

php - 如何在倒计时完成后从MySql中选择随机行?

php - 检查 key 存在于 redis 中的最快方法 - php

javascript - 数据表追加 html 但刷新了?

javascript - 将内容类型更改为 JSON 时,从数据库加载更多内容不起作用

javascript - 使用 Google Analytics 跟踪个人资料页面上的访问情况

javascript - Google map 中的平滑页面转换