2014-09-06 84 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