簡體   English   中英

顯示/隱藏適用於JQuery 1.3.2,但不適用於JQuery 1.4

[英]show/hide works with JQuery 1.3.2 but not JQuery 1.4

我有一個將數據提交到服務器的函數,然后刪除編輯UI並將其替換為常規UI。 這在JQuery 1.3.2下完美工作,但在JQuery 1.4.0下不起作用。 有任何想法嗎?

function save_edit(point_id) {
  var data = {};
  data.id = point_id;
  $.post("/foo", data, function(responseData) {
    $("#value" + point_id).show();
    $("#editval" + point_id).hide();
  }, "json");
}

jQuery 1.4對有效的JSON響應文本非常挑剔。 以下是無效的JSON(很可能是您的問題)

 {foo:'bar'}

 // Strict JSON requires quoted attributes - and the quotes must be doubles!

 {"foo": "bar"}

如果您無法輕松修復服務器端JSON,則此博客文章提到使用“文本”的解決方法。 應用於您的功能,您將獲得:

function save_edit(point_id) {
  var data = {};
  data.id = point_id;
  $.post("/foo", data, function(responseText) {
    var responseData = eval("("+responseText+")");
    $("#value" + point_id).show();
    $("#editval" + point_id).hide();
  }, "text");
}

此外,您將可以通過以下操作處理錯誤情況:

function save_edit(point_id) {
  var data = {};
  data.id = point_id;
  var $edit = $("#editval"+point_id);
  var $view = $("#value"+point_id);
  $.ajax({
    method: "POST",
    url: "/foo", 
    data: data, 
    success: function(responseData) {
      $view.show();
      $edit.hide().find('.error').remove();
    },
    error: function(XHR, textStatus, errorThrown) {
      var $err = $edit.find(".error");
      if ($err.length == 0) { $err = $("<div class='error'/>").appendTo($edit); }
      $err.text("Error: "+textStatus);
    }
  });
}   

jQuery 1.4使用嚴格的JSON解析,如發行說明中所述 您的數據有效JSON嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM