jQuery 在使用 ajax 时抓取错误的形式
jQuery grabbing wrong form when using ajax
我有一个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(),
另一种方法:将按钮click
事件替换为表单submit
事件。
$(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.
}
});
});
});
JSFiddle 演示
相关文章:
- 同源策略目的|用户数据与基本页面数据|客户端页面抓取
- VBA正在抓取不在HTML源文件中的生成内容
- 尝试使用Node.js动态路由从IMDB中抓取电影内容.但是在我的output.json文件中没有定义
- 如何从网站上抓取链接和图片
- 用jquery抓取图像SRC-attr
- Html抓取网站加载错误的J汤Java
- Ruby on Rails屏幕抓取错误-“;您必须在浏览器中启用javascript才能使用此应用程序"
- jQuery 在使用 ajax 时抓取错误的形式
- 抓取外部网站,但返回“您必须在此站点上使用支持 JavaScript 的浏览器”错误
- 原生脚本中的动态抓取?需要抓取页面,能够单击HTML按钮
- 700 由 AJAX 引起的 Google 抓取错误
- JavaScript从错误的位置抓取图像
- Facebook时间线错误3502,调试工具不会抓取localhost
- 错误:无法将数组转换为对象数据抓取脚本
- Python抓取网站得到Apache Tomcat/6.0.36 -错误报告
- 抓取Parsley JS字段错误和更新CSS
- 类似Facebook的按钮从错误的页面抓取内容
- phantomJS抓取多页会导致执行顺序错误
- 在使用HttpWebRequest进行web抓取时,如何绕过未定义的JavaScript错误
- 抓取元素的 css 属性返回错误的值