dozc1071 2014-02-09 18:06 采纳率: 100%
浏览 70
已采纳

使用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>

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;

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

  • 写回答

7条回答 默认 最新

  • douzhang1852 2014-02-09 18:14
    关注

    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:

    First Solution

    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);
        });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示