<!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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。