Javascript jQuery在使用ajax时抓取了错误的表单

Javascript jQuery在使用ajax时抓取了错误的表单,javascript,php,jquery,Javascript,Php,Jquery,我有一个php函数,在数据库中循环匹配,在循环中,有一个表单返回到调用该函数的页面: while($row=mysql_fetch_array($get_unconfirmed)) { echo "<form name='confirm_appointment' method='post' class='confirm_appointment'>"; echo "<input type='hidden' name='app_id' value='$

我有一个php函数,在数据库中循环匹配,在循环中,有一个表单返回到调用该函数的页面:

    while($row=mysql_fetch_array($get_unconfirmed))  
{ 
echo "<form name='confirm_appointment' method='post' class='confirm_appointment'>";
        echo "<input type='hidden' name='app_id' value='$appointment_id'>";
        echo "<input type='hidden' name='clinic_id' value='$clinic_id'>";
        echo "<td><input type='submit' class='update_appointment_button' value='$appointment_id'></td>";    
        echo "</form>"; 
}
但问题是,它的设置方式,无论我推“提交”的是哪一行,我总是从最后一行(表单)获取值

所以我知道问题所在,我并没有告诉jQuery使用按下的按钮从表单中获取值。我需要以某种方式使用。这可能,但似乎无法找出正确的语法


任何帮助都将被感谢

您可以像这样访问它的父窗体

data: $(this).closest(".confirm_appointment").serialize(),
data: $this.parent().parent().serialize(), 
或者类似的事情

data: $(this).closest(".confirm_appointment").serialize(),
data: $this.parent().parent().serialize(), 

另一种方法:将按钮
单击
事件替换为表单
提交
事件

$(document).ready(function () {

    $('.confirm_appointment').submit(function (e) {

        e.preventDefault();

        var url = "ajax/update_appointment_status.php";
        var serialized = $(this).serialize();

        $.ajax({
            type: "POST",
            url: url,
            data: serialized,
            success: function (data) {
                alert(data); // show response from the php script.
            }
        });
    });
});

您所有的表格都有相同的等级。您的表单将通过serialize进行处理,但每次都将被覆盖。把最后一个留给你。您需要使您的表单独一无二。->@Ding向您展示了一种相对的方法,可以通过激活的提交按钮获取表单。在另一个场景中,您可以使用数据库记录的
id
来帮助您使表单唯一。我们的想法是只获取一个表单,而不是一次获取所有表单。当将-data:$(“.confirm_appointment”).serialize()更改为-data:$(this).最接近的(.confirm_appointment”).serialize()时,数据为空这是因为HTML无效-您需要将整个表包装在
表单
元素中。罗伊-谢谢,你能再详细一点吗?谢谢你,罗伊。。。这是有道理的。我给了按钮和表单一个匹配的数据集,最后得到了数据:$(“+form_data).serialize(),。