【问题标题】:JSON Delete Table RowJSON 删除表格行
【发布时间】:2009-10-24 11:01:58
【问题描述】:

我正在尝试使用 JSON 删除一行数据,但是当我提示确认对话框时,我的 javascript 函数无法按如下方式工作:

<script type="text/javascript">
    $().ready(function() {
      $("a.delete").click(function() {
        $.ajax({
          type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
          success: function(msg) {
            if (msg.status == "ok") {
              $("tr#" + msg.id).hide();
            }
            else {
              alert(msg.exception);
            }
          }
        });

        return false;
      });
    });
</script>

上面的工作绝对没问题,但是我把以下内容放进去的那一刻:

  <script type="text/javascript">
    $().ready(function() {
      $("a.delete").click(function() {
        if (!confirm("Are you sure you want to delete this?")) return false;
        $.ajax({
          type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
          success: function(msg) {
            if (msg.status == "ok") {
              $("tr#" + msg.id).hide();
            }
            else {
              alert(msg.exception);
            }
          }
        });

        return false;
      });
    });
  </script>

这个确实进行了删除,但它并没有隐藏表格行,这让我觉得它没有被删除。有什么想法吗?

【问题讨论】:

  • 在 Firebug 中打开所有错误的 Break,看看是否有任何错误。
  • 你能放一个TR(html)看看它们是如何形成的。并且查询返回 json Ajax
  • 但是在前一个例子中是有效的,所以后者应该没有影响。
  • confirm 调用也应该没有效果,所以发生了一些有趣的事情。

标签: javascript jquery


【解决方案1】:

试试这个:

  <script type="text/javascript">
    $().ready(function() {
      $("a.delete").click(function() {
       if (confirm("Are you sure you want to delete this?")){ 
          $.ajax({
            type: "POST", contentType: "application/json; charset=utf-8", url: this.href, data: "{}", dataType: "json",
            success: function(msg) {
              if (msg.status == "ok") {
                $("tr#" + msg.id).hide();
              }
              else {
                alert(msg.exception);
              }
            }
           });
        }

        return false;
      });
    });
  </script>

希望它对你有用...

【讨论】:

    猜你喜欢
    • 2012-07-18
    • 1970-01-01
    • 2010-11-15
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    相关资源
    最近更新 更多