douya8978 2014-09-14 15:37
浏览 30
已采纳

AJAX发布多种形式

I have a list, each row is a form with a submit button. When I submit the form, data is sent through the post and must be update the div with the result. But there is a problem when sending the post, javascript does not send data correctly.

This is index file:

<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(function() {
    $(".notif-close").click(function(e) {
        e.preventDefault();
        var notif_id = $(".notif_id").val();
        var data = 'notif_id='+ notif_id;
        $.ajax({
            type: "POST",
            url: "post.php",
            data: data,
            beforeSend: function(send) {
                $("#notif_box").fadeOut(400, function() {
                    $('.loader').fadeIn(50);
                    }
                )},
            success: function(html){ // this happen after we get result
                $(".loader").fadeOut(50, function()
                { 
                    $("#notif_box").html(html).fadeIn(400);
                    $(".notif_id").val("");

                });
                return false;
            }
        });   
    });
});
</script>
</head>
<body>
    <form method='post' action='post.php'>
        <input type='hidden' id='notif_id' name='1' class='notif_id' value='1' />
        <button type="submit" id="notif-close" class="notif-close">notif-close1</button>
    </form>
    <form method='post' action='post.php'>
        <input type='hidden' id='notif_id' name='2' class='notif_id' value='2' />
        <button type="submit" id="notif-close" class="notif-close">notif-close2</button>
    </form>
    <div class='notificationsblock' id='notif_box'>Result</div>
    <span class='loader' style="display: none; position: absolute;">Please wait...</span>
</body>
</html>

And this is post.php:

 <?
 sleep(2);
 print_r($_POST);
 ?>

Help me. Please tell me what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • duandao3265 2014-09-14 15:43
    关注

    Try changing

    var notif_id = $(".notif_id").val();
    

    to

    var notif_id = $(this).parent().find(".notif_id").val();
    

    You can also try changing

    var data = { 'notif_id' : notif_id }
    

    You also have same IDs: #notif_id, #notif_close, which can (and will) cause errors and conflicts. You must give unique IDs. Giving unique names to input elements and forms is also a better idea.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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