用ajax发送表单数据

编程入门 行业动态 更新时间:2024-10-27 04:30:32
本文介绍了用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发送表单数据

本文发布于:2023-06-06 10:48:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/539005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   数据   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!