将输入值发送到 Laravel 5.4 中的 ajax

Send input value to ajax in Laravel 5.4

我有一个评论列表现在我想给大家回复,我在输入表单中使用了这个代码:

 @foreach($comments as $comment)
        <div class="questions" dir="rtl">
            {{$comment->description}}
            <div class="row">
                <div class="col-sm-2">
                    <input type="hidden" value="{{$comment->id}}" 
                     class="comment_id">
                    <button type="submit" style="float:right" 
                    class="send_answer"> send </button>
                    {{csrf_field()}}
                </div>
                <div class="col-sm-10">
                  <input class="answer" type="text" />
                </div>
            </div>
        </div>
  @endforeach

现在我在 ajax 中使用此代码,但我在控制台中给出了未定义的错误:

   $(document).on('click', '.send_answer', function (event) {
            var comment_id = $(this).find('.comment_id').val();
            var answer = $(this).find('.answer').val();
            console.log(comment_id);
            console.log(answer);

        });

我怎样才能给 Comment_id 并发送给 ajax?

使用parent()prev()得到jquery

中的comment_id
$(document).on('click', '.send_answer', function (event) {
        var comment_id = $(this).parent().find('.comment_id').val();
        var answer = $(this).closest('.questions').find('.answer').val();
        console.log(comment_id);
        console.log(answer);
});

您可以使用 closest()parent() 和单个元素 idclass:

$(document).on('click', '.send_answer', function (event) {
    var comment_id = $(this).closest('.questions').find('.comment_id').val();
    var answer = $(this).closest('.questions').find('.answer').val();
        console.log(comment_id);
        console.log(answer);
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="questions" dir="rtl">
            <div class="row">
                <div class="col-sm-2">
                    <input type="text" value="121" 
                     class="comment_id">
                    <button type="submit" style="float:right" 
                    class="send_answer"> send </button>
                </div>
                <div class="col-sm-10">
                  <input class="answer" type="text" value='answer' />
                </div>
            </div>
        </div>