append() is appending multiple times consecutively
I have set up an submit box using ajax function and when success, I have the function .append()
The problem is that it will append consecutively when I try to submit something. For example: if I submit a comment the first time, it appends correctly, then if I s开发者_如何学Pythonubmit another comment right after (without refreshing page), it will append twice, then if I submit another comment after that, it will append three times, so on and so forth. How do I prevent this from happening?
var id = $(this).attr('id');
$('#comments_'+id).keydown(function(e){
if(e.keyCode == 13)
{
e.preventDefault();
var comment = $(id).val();
var comment = 'comment='+comment;
$.ajax({
type: "POST",
url: "edit.php",
data: comment,
cache: false,
success: function(data)
{
$('#commentSelect'+id).append(data);
}
});
return false;
}
});
I set up, so I can submit on enter
Thank you for your time!
Without knowing a few more details, I would guess you are loading this javascript after each ajax request. Every time it is run it rebinds the event. You either need to load it only once per page load, or change
$('#comments_'+id).keydown(function(e){
to
$('#comments_'+id).unbind('keydown').keydown(function(e){
精彩评论