我有一个 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 来提交表单:
$( document ).ready(function() {
$(".update_appointment_button").click(function(){
var url = "ajax/update_appointment_status.php";
$.ajax({
type: "POST",
url: url,
data: $(".confirm_appointment").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
return false; // avoid to execute the actual submit of the form.
});
});
但问题是,它的设置方式,无论我按“提交”哪一行 - 我总是从最后一行(表单)获取值。
所以我知道这个问题,我没有告诉 jQuery 从按下按钮的表单中获取值。我可能需要以某种方式使用 .this,但似乎无法找出正确的语法。
任何帮助都会被应用!
最佳答案
您可以像这样访问其父表单
data: $(this).closest(".confirm_appointment").serialize(),
或者类似的东西
data: $this.parent().parent().serialize(),
关于javascript - 使用 ajax 时 jQuery 抓取错误的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28181175/