加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将form以ajax方式提交

发布时间:2020-12-16 01:00:09 所属栏目:百科 来源:网络整理
导读:!DOCTYPE html html head meta http-equiv="Content-Type" content="text/html; charset=UTF-8" / script src="/js/jquery-1.6.2.min.js"/script script jQuery.fn.extend({ ajaxForm : function (fn){ var data = {'ajax':1}; $(this).find('input[type="tex
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script src="/js/jquery-1.6.2.min.js"></script> <script> jQuery.fn.extend({ ajaxForm : function (fn){ var data = {'ajax':1}; $(this).find('input[type="text"],input[type="password"],input[type="hidden"],textarea,select:not([multiple])').each(function(i){ data[this.name] = this.value; }); $(this).find('select[multiple]').each(function(i){ var vals = []; $(this).find('option:selected').each(function(j){ vals[j] = this.value; }); data[this.name] = vals; }); $(this).find('input[type="radio"],input[type="checkbox"]').each(function(i){ var name = this.name.replace('[]',''); data[name] = '';//设置默认为空 }); $(this).find('input[type="radio"]:checked,input[type="checkbox"]:checked').each(function(i){ var name = this.name.replace('[]','['+i+']'); data[name] = this.value; }); $.ajax({ type: this.method,url: this.action,data: data,dataType: 'json',error: function(error){if(typeof(fn)!='undefined')fn.call(this,{'error':error})},success: function(json){if(typeof(fn)!='undefined')fn.call(this,json)} }); return false; } }); </script> </head> <body> <form action="/" onsubmit="return $(this).ajaxForm(function(json){alert(json)})"> <input name="test" type="text" /> <input type="submit"/> </form> </body> </html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读