将ajax与现有表单一起使用

时间:2012-09-20 08:57:38

标签: jquery ajax forms firebug web-inspector

我想用ajax处理这个表单,但对于我在发送之前应该如何处理数据并不完全清楚。这是我的表单,它是一个输出的表达式引擎模块,所以我不知道php函数会发生什么:

<form id="bookmark_form_entry_106" class="bookmark_form" name="bookmark_form" method="post" action="http://mysite.com//S=1b73e2e22729ccf0613b758ecc7e2631fab28745/">

<div class="hiddenFields">
<input type="hidden" name="XID" value="438068dba50235d9992e1492a6171e892f7bac60">
<input type="hidden" name="ACT" value="50">
<input type="hidden" name="RET"     value="http://mysite.com/S=1b73e2e22729ccf0613b758ecc7e2631fab28745/video/esegui_slq_1">
<input type="hidden" name="type" value="entry">
<input type="hidden" name="data_id" value="106">
<input type="hidden" name="site_id" value="3">
</div>


<input type="submit" value="add bookmark">

</form> 

我将使用jQuery $ .ajax();但我不知道如何处理表单数据:

$.ajax({  
type: "POST",  
url: "http://mysite.com//S=1b73e2e22729ccf0613b758ecc7e2631fab28745/",  // is this correct?
data: ,  // what data should go there?
success: function() {  
 // wohoo, this works! 
}  
});  

我是一个很新的表单,所以我不确定我是否必须知道更多关于POST脚本如何处理我的数据,或者如果知道表单内部的内容就足够了。

我也很好奇我如何使用网络检查员(或萤火虫)测试这个问题谢谢!

2 个答案:

答案 0 :(得分:3)

要获取数据,您需要jQuery的序列化函数(http://api.jquery.com/serialize/)。

var data = $('#form_id').serialize()

然后在AJAX调用中使用data变量!

根据您处理表单提交的具体情况,您可能应该将$(this)变量作为已提交的表单。

所以构建你的电话的好方法是:

$.ajax({  
type: "POST",  
url: $(this).attr('action'),  // read the action attribute of the form
data: $(this).serialize(),  // what data should go there?
success: function() {  
 // wohoo, this works! 
}  
});  

答案 1 :(得分:0)

data使用$('#bookmark_form_entry_106').serialize()设置帖子值

$.ajax({  
    type: "POST",  
    url: "http://mysite.com//S=1b73e2e22729ccf0613b758ecc7e2631fab28745/",  // is this correct?
    data: $('#bookmark_form_entry_106').serialize(),  // what data should go there?
    success: function() {  
     // wohoo, this works! 
    }  
});