Jquery ajax:如何保持记录不消失,当页面刷新


jquery ajax: how to keep record not dissapear when the page is refresh

我使用这个脚本

<script type="text/javascript">
  $(function () {
    $(".comment_button").click(function () {
      var element = $(this);
      var boxval = $("#content").val();
      var dataString = 'content=' + boxval;
      if (boxval == '') {
        alert("Please Enter Some Text");
      } else {
        $("#flash").show();
        $("#flash").fadeIn(400).html('<img src="ajax.gif" align="absmiddle">&nbsp;<span class="loading">Loading Update...</span>');
        $.ajax({
          type: "POST",
          url: "update_data.php",
          data: dataString,
          cache: false,
          success: function (html) {
            $("ol#update").prepend(html);
            $("ol#update li:first").slideDown("slow");
            document.getElementById('content').value = '';
            $("#flash").hide();
          }
        });
      }
      return false;
    });
    $('.delete_update').live("click", function () {
      var ID = $(this).attr("id");
      var dataString = 'msg_id=' + ID;
      if (confirm("Sure you want to delete this update? There is NO undo!")) {
        $.ajax({
          type: "POST",
          url: "delete_data.php",
          data: dataString,
          cache: false,
          success: function (html) {
            $(".bar" + ID).slideUp('slow', function () {
              $(this).remove();
            });
          }
        });
      }
      return false;
    });
  });
</script>

这个脚本结合使用jquery和ajax实时更新和删除记录

问题是当我刷新页面时,记录会消失。如何保持显示的记录不消失,当页面重新加载?

首先查看评论列表。您是否在查询中设置了任何限制,如果有,则使用Order by Primary ID des.这样它将首先显示最新的记录。

当你删除任何记录时,检查它是否真的从数据库中删除了。因为您没有按照您给出的代码检查记录是否实际删除。

假设您正在使用update_data.php和delete_data.php操作某些数据库,您可以使用PHP最初使用数据库上的当前数据来呈现页面。

如果不是这种情况,并且您无法访问该数据库(它可能是第三方web服务,对吗?),或者您出于任何原因不想这样做,您可以使用cookie,如Saeed answers。