【发布时间】:2013-05-19 18:44:10
【问题描述】:
我的页面上有多个表单。当我单击表单提交按钮时,我想通过 ajax 仅发送该表单的表单值。这就是我所拥有的。第一个表单按预期工作,第二个表单实际提交表单。如何单独定位每个表单。我觉得我应该在某个地方使用 .find()。
<form id="form1" method="post">
<input type="text" id="name1" name="value" value="">
<input type="submit" id="update_form" value="Save Changes">
</form>
<form id="form2" method="post">
<input type="text" id="name2" name="value" value="">
<input type="submit" id="update_form" value="Save Changes">
</form>
<script>
// this is the id of the submit button
$("#update_form").click(function() {
$.ajax({
type: "POST",
url: "approve_test.php",
data: $(this.form).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.
});
</script>
【问题讨论】:
-
不要对元素使用相同的 id。
-
@Skelly 我确实看到了,但他想用一个按钮提交多个表单。每个表单都有一个提交按钮。