2016-05-13 59 views
3

我是上single button單擊。通過Ajax單擊提交多個表單

例如假設我有兩種形式。在按一下按鈕,我可以看到$('form[id^=buyerForm]').length gives 2.

first iteration選秀權first form data,讓Ajax調用,但也second iterationpicks the first form dataThis 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> 
+0

請加HTML部分 – brk

+1

** ID應該永遠是唯一的**這就是爲什麼你只能得到第一個數據,因爲其他形式是重複的ID只能用類而不是 – guradio

回答

1

永遠不要使用循環NEVER IDS:

$("#jPdetails").on('click', function() { 
     $('form[id^=buyerForm]').each(function(i,v) { 
      post_form_data($(v).serialize(),v); 
     }); 
    }); 

     function post_form_data(data,el) { 
      var jsellerAddress = $(el).find("#jsellerAddress").val(); 
      var jagentId = $(el).find("#jagentId").val(); 
      var jbuilding = $(el).find("#jbuilding").val(); 
      var junitId = $(el).find('#junitId option:selected').val(); 
      var jpropertyAed = $(el).find("#jppropertyAed").val(); 
      var jparkingBaysAt = $(el).find("#jparkingBaysAt").val(); 
      var jtotalAed = $(el).find("#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); 
       } 
      }); 
     }; 

或改變所有的ID,以類

$("#jPdetails").on('click', function() { 
     $('.buyerForm').each(function(i,v) { 
      post_form_data($(v).serialize(),v); 
     }); 
    }); 

     function post_form_data(data,el) { 
      var jsellerAddress = $(el).find(".jsellerAddress").val(); 
      var jagentId = $(el).find(".jagentId").val(); 
      var jbuilding = $(el).find(".jbuilding").val(); 
      var junitId = $(el).find('.junitId option:selected').val(); 
      var jpropertyAed = $(el).find(".jppropertyAed").val(); 
      var jparkingBaysAt = $(el).find(".jparkingBaysAt").val(); 
      var jtotalAed = $(el).find(".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); 
       } 
      }); 
     }; 

或:

$("#jPdetails").on('click', function() { 
    $('form[id^=buyerForm]').each(function(i,v) { 

     $.ajax({ 
      type: 'POST', 
      url: 'jointpurchasehandller.php', 
      data:$(v).serialize(), 
      success: function(result) { 
       alert(result); 
      }, 
      error: function(error) { 
       alert(error); 
      } 
     }); 

    }); 
}); 
+0

不知道你的回答是否正確?但你的代碼不適合我。 – simbada

+0

你是什麼意思的「不適合我」你有什麼錯誤,提供的HTML,我說永遠不會在循環中使用id? – madalinivascu

+0

哦,非常抱歉。你的解決方案爲我工作。我只是重新檢查了你的代碼。但是,你可以擴大這個行的v,以更好地理解你的答案。 post_form_data($(v)的.serialize(),V); – simbada

0

我認爲你可以將代碼大小減少到

$("#jPdetails").on('click', function() { 
    $forms=$('form[id^=buyerForm]'); 
    $($forms).each(function(index) { 
     // this will bind corresponding data for each form 
     dataString=$($forms[index]).serialize(); 
     $.ajax({ 
      type: 'POST', 
      url: 'jointpurchasehandller.php', 
      data: dataString, 
      success: function(result) { 
       alert(result); 
      }, 
      error: function(error) { 
       alert(error); 
      } 
     }); 

    }); 
}); 
+0

如果沒有工作,請讓我知道 –

+0

當然,讓我檢查。謝謝 – simbada

+0

使用與'jsellerAddress','jagentId'相同的字段名稱...在表單中列出 –