2013-09-24 23 views
0
$(".form").click(function(){ 

     $.post(
      '/forms/ajax.php',{ 
      id:form.id.value 
      }, 
      function(output){ 
      $('#ajax').html(output).fadeIn(50)}, 500); 
      } 
     ); 


$n=1; 
while(//LOOP ALL LIST FROM DB){ 
    echo" 
     <form id='form' class='form'> 
     <div class='content'>".$n.". ".$title[$n-1]."</div> 
     <input type='hidden' name='id' value='".$id."'/> 
     </form> 
    "; 
    $n++ 
} 

我有一個網頁使用while循環迴路的所有列表,每個列表可以比jQuery的點擊將張貼到另一個數據&頁面後回數據。多形式共享一個jQuery的崗位

我的問題是如何使用一個$ post腳本的所有列表。

ex。如果list_1單擊,它會發布list_1的數據。如果list_3點擊,它會發布list_3的數據

回答

0

其實很簡單。 this引用了dom元素,因此諸如$(this).submit()$(this).serialize()之類的調用將發佈表單或爲表單的內容提供序列化的字符串。

$('.form').on('submit', function(e) { 
    e.preventDefault(); // prevents form from submitting 

    $.post('/form/ajax.php', { $(this).serialize() }, function(output) { 
     $('#ajax').html(output).fadeIn(50); 
    }); 
}); 

告警序列化的數據的例子可以在this fiddle

可以找到