2011-02-13 41 views
24

我有一個表格,例如:jQuery的 - 發送一個形式異步

<form action='???' method='post' name='contact'> 
     <input type="text" class="inputContact" name="mittente" /> 
     <textarea class="textContact" name="smex"></textarea> 
     <input type="submit" value="Send" /> 
    </div> 
</form> 

我想異步發送這些數據,trought jQuery函數$.ajax

編輯:與解決方案:

<form name='contactForm'> 
    <input type="text" class="inputContact" name="mittente" /> 
    <textarea class="textContact" name="smex"></textarea> 
    <input type="submit" value="Send" /> 
</form> 

<script type="text/javascript"> 
$(document).ready(function() { 
    $('form[name=contactForm]').submit(function(e){ 
     e.preventDefault(); 
     $.ajax({ 
      type: 'POST', 
      cache: false, 
      url: './ajax/header_ajax.php', 
      data: 'id=header_contact_send&'+$(this).serialize(), 
      success: function(msg) { 
       $("#boxContentId").html(msg); 
      } 
     }); 
    });  
});   
</script> 
+0

究竟是什麼 你要知道?您已經知道您必須(或想要)使用`$ .ajax`。我想你已經閱讀過它的文檔。你卡在哪裏? – 2011-02-13 15:48:28

+0

我想知道如何通過點擊「發送」按鈕來執行$ .ajax – markzzz 2011-02-13 15:51:25

回答

35
$('form[name=contact]').submit(function(){ 

    // Maybe show a loading indicator... 

    $.post($(this).attr('action'), $(this).serialize(), function(res){ 
     // Do something with the response `res` 
     console.log(res); 
     // Don't forget to hide the loading indicator! 
    }); 

    return false; // prevent default action 

}); 

參見: