以下是我的代码:
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内容。但不幸的是,我无法取代这些内容。我不想重新加载页面。应该替换内容而不重新加载页面。 有人可以在这方面指导我吗? 提前谢谢。
答案 0 :(得分:1)
尝试
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);