Форум сайта python.su
Необходимо с помощью ajax добавлять и удалять сообщения. Использую jquery и следующий код:
<script type="text/javascript">
function addProductReview(e, functn){
e.preventDefault();
var review_form = jQuery(e.target);
jQuery.ajax({
url: review_form.attr('action'),
type: review_form.attr('method'),
data: review_form.serialize() + review_form.find('input').attr('name') ,
dataType: 'json',
success: function(json){
if (json.success == "True"){
functn(json);
}
},
error: function(xhr, ajaxOptions, thrownError){
alert('Error')
}
});
};
function Remove_message(json){
//alert(json.name);
var inpt = $("input[name=" + json.name + "]").parent()
inpt.parent().parent().remove();
//alert("remove");
}
function Add_message_to_list(json){
$(json.alltopic).each(function(index) {
var tds = '<div class="news-line-right">';
tds+='<div class="news-line"><img src="/' + json.alltopic[index][1] + '" id="image-style"></div>';
tds+='<div class="news-line">' + json.alltopic[index][0] + '</div>';
tds+='<div class="news-line"><form action="{% url del_message %}" method="post" class = "bttn"><input type="submit" value="Del ' + json.alltopic[index][2] + '" name="' + json.alltopic[index][2] + '" ></form></div>';
$('#html_add div:last').after(tds);
});
}
$(document).ready(function(){
$("form#review").submit(function(e){
addProductReview(e, Add_to_message);
});
$("form.bttn").submit(function(e){
addProductReview(e, Remove_message);
});
});
</script>
<div class="news-line-right">
<div class="news-line"><img src="/{{ news.1 }}" id="image-style"></div>
<div class="news-line">{{ news.0 }}</div>
<div class="news-line">
<form action="{% url del_message %}" method="post" class = "bttn">
<input type="submit" value="Del {{ news.2 }}" name="{{ news.2 }}">
</form>
</div>
</div>
Офлайн