我是上single button
单击。通过Ajax单击提交多个表单
例如假设我有两种形式。在按一下按钮,我可以看到$('form[id^=buyerForm]').length gives 2.
的first iteration
选秀权first form data
,让Ajax调用,但也second iteration
picks the first form data
。 This is the problem.
任何人都可以请解释为什么迭代总是选择第一种形式的数据?
<script type="text/javascript">
$("#jPdetails").on('click', function() {
$('form[id^=buyerForm]').each(function() {
post_form_data($(this).serialize());
});
});
function post_form_data(data) {
var jsellerAddress = $("#jsellerAddress").val();
var jagentId = $("#jagentId").val();
var jbuilding = $("#jbuilding").val();
var junitId = $('#junitId option:selected').val();
var jpropertyAed = $("#jppropertyAed").val();
var jparkingBaysAt = $("#jparkingBaysAt").val();
var jtotalAed = $("#jtotalAed").val();
var dataString =
'jsellerAddress=' + jsellerAddress +
'&jagentId=' + jagentId +
'&jbuilding=' + jbuilding +
'&junitId=' + junitId +
'&jpropertyAed=' + jpropertyAed +
'&jparkingBaysAt=' + jparkingBaysAt +
'&jtotalAed=' + jtotalAed;
$.ajax({
type: 'POST',
url: 'jointpurchasehandller.php',
data: dataString,
success: function(result) {
alert(result);
},
error: function(error) {
alert(error);
}
});
};
</script>
HTML 我的HTML结构
<div id="jointBuyer" class="JointBuyerDive" style="display:none">
<div id="jBuyer">
<div id="inner"class="col-lg-6">
<form id="buyerForm" role="form" method="POST" enctype="multipart/form-data">
</div>
</div>
<div>
和我加入multiple forms
以下方式
<script
function addBuyer() {
$("#addBuyer").click(function() {
$("#buyerForm").clone().appendTo("#jointBuyer");
});
}
</script>
请加HTML部分 – brk
** ID应该永远是唯一的**这就是为什么你只能得到第一个数据,因为其他形式是重复的ID只能用类而不是 – guradio