2016-12-22 19 views
5

Tıklandığında yeni bir form oluşturmak için NEWFORM etiketli bir düğme vardır. Her formun bir gönderme düğmesi vardır. Her formun gönder düğmesine tıklandığında, bu formun değerleri AJAX üzerinden gönderilecektir. Kodum ilk kez iyi çalışıyor, ancak yeni bir form oluşturulduğunda ve sunulduğunda, tüm formların tüm değerleri birlikte gönderilecektir.Eklenen formu ayrı olarak gönderme

İşte benim snippet'tir:

$(document).ready(function() { 
 
    $(".newform").click(function() { 
 
    $(".MyForm") 
 
    .eq(0) 
 
    .clone() 
 
    .show() 
 
    .insertAfter(".MyForm:last"); 
 
    }); 
 

 
    $(document).on('click', '.MyForm button[type=submit]', function(e) { 
 
    e.preventDefault() // To make sure the form is not submitted 
 
    $('.MyForm').each(function() { 
 
    console.log($(this).serialize()) 
 
     $.ajax(
 
     $(this).attr('action'), 
 
     { 
 
      method: $(this).attr('method'), 
 
      data: $(this).serialize() 
 
     } 
 
    ) 
 
    }); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> 
 
<span class="newform">NEWFORM+</span> 
 
<div class="all"> 
 
    <form class="MyForm" method="post"> 
 
    <input type="text" placeholder="name" value="Aynaz" name="a1" /> 
 
    <select name="Avg"> 
 
     <option value="1">1</option> 
 
     <option value="2">2</option> 
 
    </select> 
 
    <button type="submit">Submit</button> 
 
    </form> 
 
</div>

+0

arasında içeriksel uygulama id nitelikleri ile bunları birbirinden ayırır ve bunların formu içe olduğu bu – Jigar7521

+0

gönderilen gider zaman bu kimliği geçti mi? – brk

+0

hayır yuvalanmış değiller – inaz

cevap

3

Önce sen onClick doğru formu belirlemek gerekir, bu yüzden hepsi sunulduktan sonra, çözeltide tüm ".MyForm" nesneleri yineleme, ve gönderin:

$(document).ready(function() { 
 
    $(".newform").click(function() { 
 
    $(".MyForm") 
 
    .eq(0) 
 
    .clone() 
 
    .show() 
 
    .insertAfter(".MyForm:last"); 
 
    }); 
 

 
    $(document).on('click', '.MyForm button[type=submit]', function(e) { 
 
    e.preventDefault() // To make sure the form is not submitted 
 
    var $frm = $(this).closest('.MyForm'); 
 
    console.log($frm.serialize()); 
 
    $.ajax(
 
     $frm.attr('action'), 
 
     { 
 
      method: $frm.attr('method'), 
 
      data: $frm.serialize() 
 
     } 
 
    ); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> 
 
<span class="newform">NEWFORM+</span> 
 
<div class="all"> 
 
    <form class="MyForm" method="post"> 
 
    <input type="text" placeholder="name" value="Aynaz" name="a1" /> 
 
    <select name="Avg"> 
 
     <option value="1">1</option> 
 
     <option value="2">2</option> 
 
    </select> 
 
    <button type="submit">Submit</button> 
 
    </form> 
 
</div>

+0

Hemen hemen aynı bir cevap göndermeye hazırlanıyordum, bunun yerine size unutacağım :) –

+0

@RobM., Teşekkürler, benim versiyonumda hiçbir şey kaçırmadım umarım) – 2oppin

+0

Çok teşekkür ederim! – inaz

0
  $(document).ready(function() { 
       $(".newform").click(function() { 
       $(".MyForm") 
       .eq(0) 
       .clone() 
       .show() 
       .insertAfter(".MyForm:last"); 
       }); 

       $(document).on('click', '.MyForm button[type=submit]', function(e) { 
       e.preventDefault() // To make sure the form is not submitted 
       var $this = $(this).closest("form"); 
       console.log($this.serialize()) 
        $.ajax(
        $(this).attr('action'), 
        { 
         method: $this.attr('method'), 
         data: $this.serialize() 
        } 
       ) 
       }); 
      }); 
0

Bunu yapabilirsiniz ziyade

$(document).on('submit', '.myForm', function(e) { 
    e.preventDefault() 
    $.ajax({ 
     type: 'post', 
     data: $(this).serialize(), 
     url: 'submit.php' 
    }) 
}) 

sorun $(this)

İlgili konular