zoukankan      html  css  js  c++  java
  • 将form转为ajax提交的js代码

    参考网络代码基础上进行修改,调试通过。

    在html中插入下面的代码:

    函数ajaxSubmit是submit的ajax形式。

    注意:这里面使用到了jquery库

                //<!--将form中的值转换为键值对。-->
                function getFormJson(frm) {
                    var o = {};
                    var a = $(frm).serializeArray();
                    $.each(a, function () {
                        if (o[this.name] !== undefined) {
                            if (!o[this.name].push) {
                                o[this.name] = [o[this.name]];
                            }
                            o[this.name].push(this.value || '');
                        } else {
                            o[this.name] = this.value || '';
                        }
                    });
    
                    return o;
                }
    
                //<!--将form转为AJAX提交。-->        
           //<!--fn为返回后的回调函数,可以为空-->
    function ajaxSubmit(frm, fn) { var dataPara = getFormJson(frm); var ajax_para={}; ajax_para['data']=dataPara; if (!frm.attr('action') ){ ajax_para['url']=this.location.pathname; } else{ ajax_para['url']=frm.attr('action'); } ajax_para['type']=frm.attr('method'); if (!frm.attr('method')){ ajax_para['type']='GET'; } if (fn) { ajax_para['success']=fn; } htmlobj=$.ajax(ajax_para); return htmlobj; }

    在form提交时候调用该插件

    将form中submit按钮修改成

    <button type="button" class="btn btn-default" onclick="ajaxSubmit($('form'))">submit</button>
  • 相关阅读:
    FTP Protocol
    File Operations
    Ubuntu Install Chinese Input Method
    Vim Intro
    ISA Introduction
    Fourier Transform
    Process/Thread Synchronization
    Process Synchronization-Example 2
    leetcode 栈和队列类型题
    leetcode 字符串类型题
  • 原文地址:https://www.cnblogs.com/yasmi/p/4787978.html
Copyright © 2011-2022 走看看