2010-11-03 30 views
0

嗨,我使用下面的代碼來獲取數據並將其發送到一個PHP頁面,它工作正如我想要的但它只發送第一個「.order」它遇到,即。我需要發送每個元素與class =「order」,這是某種.each()?通過AJAX發送每個類名稱的元素到php

$('#submit').click(function(){ 
        var order=$('.order').html(); 
        var dataString = 'order='+ order; 

        $.ajax 
         ({ 
         type: "POST", 
         url: "order.php", 
         data: dataString, 
         cache: false, 
         success: function(html) 
          { 
           $("#response").html(html); 
          } 
         }); 
      }); 

這樣做,而現在它的工作,離奇!

$('#submit').live('click',function(){ 
       var order=$('.order').text(); 
       var dataString = 'order='+ order; 

       $.ajax 
        ({ 
        type: "POST", 
        url: "order.php", 
        data: dataString, 
        cache: false, 
        success: function(html) 
         { 
          $("#response").html(html); 
         } 
        }); 
     }); 

回答

2

嘗試var order = $('form').serialize()這裏解釋http://api.jquery.com/serialize/

除此之外,你必須做一些事情,如:

$('.order').each(function(){ 
    // Get values from order here... something like: order += $(this).html(); 
    // Also, note that if '.order' are inputs you should use $(this).val() instead of $(this).html(); 
}); 

希望它幫助。

1

我相信這應該這樣做:

var order=$('.order') 
    .map(function(){ return this.innerHtml; }) 
    .get().join(''); 
var dataString = 'order='+ order; 
+0

您需要對請求進行編碼。最好的方法是做'var dataString = {「order」:order};'並讓jQuery處理編碼。 – lonesomeday 2010-11-03 15:17:25