繁体   English   中英

在单击提交按钮jQuery的当前表单之后选择下一个div

[英]Select the next div after the current form on click submit button jQuery

我试图让带班下一个DIV comment-replies当前表单后prepend数据。 页面和表单上有更多具有相同类的div。

<div class="hide">
                <br>
                <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"],ENT_QUOTES,"utf-8")?>" method="post">


            <div class="form-group">
                <textarea class="form-control comment" rows="6" name="comment"></textarea>
            </div>
            <div class="form-group">
                <div class="col-sm-3" style="padding-left: 0px;">
                    <input hidden class="reply-comment-id" value="23">
                    <input class="form-control btn-xs add-reply" type="submit" name="reply" value="REPLY">
                </div>
            </div>
        </form>
            </div>
<div class="comment-replies">
               <!--replies-->
               <?php $this->get_replies($comment['comment_id']); ?>
            </div>

按钮的jQuery代码。

$('.add-reply').on('click', function (e) {
    e.preventDefault();
    var upId = $('#upload-id').val();
    var usId = $('#user-id').val();
    var cId = $(this).prev('input').val();
    var the_form = $(this).closest("form");
    var textComment = the_form.find('textarea[name="comment"]');
    var com = textComment.val();
    $.post('add_comment.php',{cid:cId,upId:upId,usId:usId,reply:com},function(data){
        the_form.nextUntil('div').prepend(data);
    //$('.comment-replies').prepend(data); this append to every div with the current class.
        the_form.addClass('hide');
    });

});

jsfiddle上的示例

这是对我有用的代码。

$('.add-reply').on('click', function (e) {
    e.preventDefault();
    var upId = $('#upload-id').val();
    var usId = $('#user-id').val();
    var cId = $(this).prev('input').val();
    var the_form = $(this).closest(".myDiv");
    var textComment = the_form.find('textarea[name="comment"]');
    var com = textComment.val();
    //$.post('add_comment.php',{cid:cId,upId:upId,usId:usId,reply:com},function(data){
    var data = 'Dummy data';
        the_form.next('.comment-replies').prepend(data);
    //$('.comment-replies').prepend(data); this append to every div with the current class.
      //  the_form.addClass('hide');
    //});

});

https://jsfiddle.net/bhdk9s28/4/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM