douzhun5971 2014-02-19 08:37
浏览 59
已采纳

访问Ajax中的Post变量

I have a commenting system that uses ajax.

It generates a div with comments and a reply box via a php loop, meaning the form is repeated over and over again. How do I get var parent to only take the data from the form I submitted? not all of the form fields with the id parent.

$(function () {
    $('.reply').on('submit', function (e) {

        $.ajax({
            type: 'post',
            url: '/app/reply',
            data: $(this).serialize(),
            success: function (data) {
                var parent = $('#parent').val();
                $("#comment_"+parent).append(data);
                var form = document.getElementById("form_" + parent);
                form.reset();
            }
        });
        e.preventDefault();
    });
});
<form action="" method="POST" class="reply" id="form_<?php echo $comment['id'] ?>">
    <textarea id="textArea"  name="comment"></textarea>
    <input type="submit" name="upload_btn" value="Reply">
    <input type="hidden" value="<?php echo $post ?>" name="post">
    <input type="hidden" value="<?php echo $comment['id'] ?>" id ="parent" name="parent">
    <input type="hidden" value="pop" name="location">
    <input type="hidden" value="<?php echo $level ?>" name="level">

</form>
  • 写回答

3条回答 默认 最新

  • duan41497 2014-02-19 08:46
    关注

    First remove id=parent from hidden field then you can access like this;

    <script>
    $('.reply').on('submit', function (e) {
        var that    =   this;
        $.ajax({
            type: 'post',
            url: '',
            data: $(this).serialize(),
            success: function (data) {
                var parent = $(that).find('input[name=parent]').val();
                //your code
            }
        });
        e.preventDefault();
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数