0

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;
    }
});​
4

2 回答 2

1

我看不出这段代码有什么问题。就像您每次都在重新绑定 keyup 事件,并且该事件只是多次绑定到评论框。

于 2012-08-10T13:33:09.313 回答
0

在我的 iPad 上写作,所以测试任何东西都不容易,但你可以这样做:

改变

$('body').on('keyup','#stg_cmtarea',

$('body').on('submit','#yourformid',

然后,您应该删除 e.which-part。如果你真的想在 enter-push 上提交,你可以创建一个在 enter 上监听的函数。

这应该可以解决您的问题。请记住始终返回 false,因此不会提交表单。

于 2012-08-10T13:19:47.957 回答