duan2891 2014-09-08 04:58
浏览 11
已采纳

too long

I am working on discussion that works like a forum. There can be several posts and users are supposed to comment.

I have this html post comment form which repeats itself after every post

 <form method="POST" action="/discussion/post-comment" id="post-comment-form" class="form-horizontal subcommentcofrm" role="form">
   <div class = "form-group">
        <div class = "col-sm-6">
            <input type = "text" class = "form-control comment-yako" id = "comment-field" placeholder = "Type your comment here..." autocomplete="off">

        </div>
   </div>
</form>

And this is my JQuery code with ajax submission:

    $("input.comment-yako").live('keypress', function(e) {
        if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) {
            console.log('heey');
             $(this).closest('form').submit(function(e){
                console.log('heeeeeeey');
                var data = $(this).serializeArray();
                var url = $(this).attr("action");
                $.ajax({
                    url: url,
                    type: 'POST',
                    cache: false,
                    data: data,
                    success: function(data) {
                        var item = $(data).hide().fadeIn(800);

                        $('.disc-content-reply').append(item);

                    },
                    error: function(e) {
                        alert(e);
                    }
                });
                e.preventDefault();
            });

            $(this).closest(".comment-yako").val("");
            return false;
        } else {
            return true;
        }
    });

Submission is supposed to take place when the user hits enter key. My problem is, when I hit enter, the form does not submit. I do not know where the problem is; what am I doing wrong?

Any help will be much appreciated.

Thanks

  • 写回答

1条回答 默认 最新

  • douchao1879 2014-09-08 05:11
    关注

    You should send the AJAX request in the keypress handler itself, not in a submit handler that you bind after the keypress occurs. Most of the time it's wrong to bind one event handler inside another event handler; if you find yourself doing that, think very hard about whether it's really what you want to do.

    In addition to changing that, I've converted the .live() call to .on(). I also changed from .serializeArray() to .serialize(), as this is the normal way to send form fields.

    $(document).on('keypress', 'input.comment-yako', function(e) {
        if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) {
            console.log('heey');
            var form = $(this).closest('form');
            var data = form.serialize();
            var url = form.attr("action");
            $.ajax({
                url: url,
                type: 'POST',
                cache: false,
                data: data,
                success: function(data) {
                    var item = $(data).hide().fadeIn(800);
                    $('.disc-content-reply').append(item);
                },
                error: function(e) {
                    alert(e);
                }
            });
            e.preventDefault();
            $(this).val("");
            return false;
        } else {
            return true;
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算