用ajax发送表单数据 [英] Send form data using ajax

查看:137
本文介绍了用ajax发送表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要发送的所有输入与阿贾克斯。我的表单有一个表格是这样的。

I want to send all input in a form with ajax .I have a form like this.

<form action="target.php" method="post" >
<input type="text" name="lname" />
<input type="text" name="fname" />
<input type="buttom" name ="send" onclick="return f(this.form ,this.form.fname ,this.form.lname) " >
</form>

而在.js文件中,我们有以下code:

And in .js file we have following code :

function f (form ,fname ,lname ){
att=form.attr("action") ;
$.post(att ,{fname : fname , lname :lname}).done(function(data){
alert(data);
});
return true;

但这不是working.i不想使用表格的内容

But this is not working.i don't want to use Form data .

推荐答案

据我们希望把所有具有name属性的表单输入字段,你可以做到这一点对所有形式的,无论字段名:

as far as we want to send all the form input fields which have name attribute, you can do this for all forms, regardless of the field names:

首个解决方案

function submitForm(form){
    var url = form.attr("action");
    var formData = {};
    $(form).find("input[name]").each(function (index, node) {
        formData[node.name] = node.value;
    });
    $.post(url, formData).done(function (data) {
        alert(data);
    });
}

第二个解决方案:在这个解决方案,您可以创建输入值的数组:

Second Solution: in this solution you can create an array of input values:

function submitForm(form){
    var url = form.attr("action");
    var formData = $(form).serializeArray();
    $.post(url, formData).done(function (data) {
        alert(data);
    });
}

这篇关于用ajax发送表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆