开发者

append() is appending multiple times consecutively

开发者 https://www.devze.com 2023-04-06 05:15 出处:网络
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 subm

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){
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号