为什么我无法替换 AJAX 成功响应中的 HTML 内容?



以下是我的代码:

jQuery AJAX函数代码:

$('#form').submit(function(e) {
  var form = $(this);
  var br_id = $('#brand_id').val();
  var status = '0';
  var module_url = $('#module_url').val();
  var upload_url = $('#upload_url').val();
  var formdata = false;
  if(window.FormData) {
    formdata = new FormData(form[0]);
  }
  var formAction = form.attr('action');
  $.ajax({
    type        : 'POST',
    url         : 'manufacturers.php',
    cache       : false,
    data        : formdata ? formdata : form.serialize(),
    contentType : false,
    processData : false,
    success: function(response) { 
      if(response.error == 0) {  
        $('#messages').addClass('alert alert-danger').text(response.error_message);
      } else { 
        $('#BrandImageModal').modal('hide');
        var newdiv = '<a href="#" id="promotion_status_'+br_id+'"><button type="button" class="btn btn-default">Off</button></a>';
        var newtd  = '<img src="'+upload_url+text(response.image_path)+'" width="80" height="80">';
        var $div = $(newdiv); // use jQuery to parse the HTML
        $div.click(function(e) { // use jQuery for event listeners
          change_promotion_status(module_url, promotion_status, br_id, '0');
          return false;
        });
        $("div #brand_"+br_id ).html( newdiv );
        $("td #brand_image_td_"+br_id ).html( newtd );       
      }
    },
    dataType:"JSON"
  });
  e.preventDefault();
});

我想用某些ID更改和的HTML内容。但不幸的是,我无法替换这些内容。我不想重新加载页面。内容应该在不重新加载页面的情况下被替换。有人能在这方面指导我吗?提前谢谢。

尝试

var newdiv = '<a href="#" id="promotion_status_' + br_id + '"><button type="button" class="btn btn-default">Off</button></a>';
//here there was no method called `text` causing a script error
var newtd = '<img src="' + upload_url + text(response.image_path) + '" width="80" height="80">';
var $div = $(newdiv); // use jQuery to parse the HTML
$div.click(function (e) { // use jQuery for event listeners
    change_promotion_status(module_url, promotion_status, br_id, '0');
    return false;
});
//the click event is bound to the dom element in $div not to the string newdiv
$("div #brand_" + br_id).html($div);
$("td #brand_image_td_" + br_id).html(newtd);

最新更新