我是上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