2014-09-06 100 views
1

我是MooTools的新手,並嘗試向表單內容發送帶有Ajax請求的url。Mootools的發佈請求

<form method="post" enctype="multipart/form-data" action="<?= $PHP_SELF; ?>" name="fn" id="fn"> 
<input name="user_name" id="user_name"> 
<input name="user_mail" id="user_name"> 
<input name="user_text" id="user_name"> 
<input type="file" name="attach"> 
<input id="button" type="button" value="Submit"> 
</form> 

<script type="text/javascript"> 
    $('button').addEvent('click', function(event) { 
     var req = new Request({ 
      method: 'post', 
      url: 'url.php', 
      onSuccess: function(response) { 
       alert(response); 
      }); 
    }); 
</script> 

當我點擊按鈕,沒有任何反應。如何從表單傳輸數據?

回答

0

您的代碼看起來不錯,你有一個}失蹤,但除了您剛纔忘了加一個.send();

req.send();,實際上,你可以傳遞數據作爲參數傳遞給方法。

測試並檢查here about the .send() method

Suggention你的代碼怎麼會是這樣的:

<script type="text/javascript"> 
    var req = new Request({ 
     method: 'post', 
     url: 'url.php', 
     onSuccess: function(response) { 
      alert(response); 
     } // < --- You actually missed this one 
    }); 

    $('button').addEvent('click', function(event) { 
     req.send(); 
    }); 
</script> 
+0

優秀作品。如何最好地從整個表單傳輸數據? – serezha93 2014-09-06 12:16:40

+1

@gladooo,那麼你可以[檢查這個答案](http://stackoverflow.com/questions/2166042/how-to-convert-form-data-to-object-using-mootools)。稍後我會在稍後更新。 – Sergio 2014-09-06 15:27:29