Reputation: 1472
I am working on a gallery with comment system. It means when I click on next and previous then the new pic with its comments are loaded. But the problem is that I am using a commentbox in which when I open a pic and submit comment for it, it submit the same comment only one time (the way it should work), but when I click on next pic (pic 2) and submit comment for it, the comment is submitted two times (instead of one), and the third pic three times and so on. Here is my jquery code hope someone help me with this
$('body').on('keyup', '#stg_cmtarea', function(e) {
if (e.which == 13 && $.trim(this.value).length) {
var picId = $(".gal_cmt_cnt_info").attr('gdata');
var userId = $("#mipicview-overlay-content").attr('vwid');
var comment = $("#stg_cmtarea").val();
var dataString = 'comment=' + comment + '&pic_id=' + picId + '&user_id=' + userId;
if ($.trim(comment).length == 0) {
alert("Please Enter Comment Text");
}
else {
$.ajax({
type: "POST",
url: "modules/gallery/piccomment_ajax.php",
data: dataString,
cache: false,
success: function(html) {
alert(html);
}
});
}
return false;
}
});
Upvotes: 0
Views: 140
Reputation: 2008
I don't see anything wrong with this code. It shoulds like you are rebinding the keyup event every time, and the event is just getting bound to the comment box too many times.
Upvotes: 1
Reputation: 14863
Writing on my iPad, so it's not easy to test anything, but you could tyr this:
Change
$('body').on('keyup','#stg_cmtarea',
With
$('body').on('submit','#yourformid',
You should then remove the e.which-part. You could make a function that listens on enter if you really want submit on enter-push.
This should fix your problem. Remember to always return false, so the form is not submitted.
Upvotes: 0