ajaxForm提交,代码片段(附常用版本下载)
做项目时经常用的到东西
$(function() { var _form = $("#xinda-form"), _btn = $("#submit-btn"), _text = _btn.text(); _form.submit(function() { _form.ajaxSubmit({ url: "后台接受的URL", type: "post", dataType: "json", beforeSubmit: function() { //提交前校验 var username = $("input[name='member[username]']").val(), password = $("input[name='member[password]']").val(); if (!username) { alert("请输入用户名"); $("input[name='member[username]']").focus(); return false; } if (!password) { alert("请输入密码"); $("input[name='member[password]']").focus(); return false; } _btn.attr("disabled", "disabled").text("正在提交..."); }, success: function(data) { //成功后返回 if (data.success == true) { alert(data.message); _form.resetForm(); } else { alert(data.message); } _btn.removeAttr("disabled").text(_text); } }); return false; }); });
其他写法(自动中断普通submit提交):
$(function() { var _form = $("#xinda-form"), _btn = $("#submit-btn"), _text = _btn.text(); var submit_option = { url: "后台接受的URL", type: "post", dataType: "json", beforeSubmit: function() { //提交前校验 var username = $("input[name='member[username]']").val(), password = $("input[name='member[password]']").val(); if (!username) { alert("请输入用户名"); $("input[name='member[username]']").focus(); return false; } if (!password) { alert("请输入密码"); $("input[name='member[password]']").focus(); return false; } _btn.attr("disabled", "disabled").text("正在提交..."); }, success: function(data) { //成功后返回 if (data.success == true) { alert(data.message); _form.resetForm(); } else { alert(data.message); } _btn.removeAttr("disabled").text(_text); } }; _form.ajaxForm(submit_option); });
下载地址:jquery.form.zip