点击登录按钮后,即触发form表单的提交事件,数据传输至后端,由后端控制页面跳转和数据。<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="login test">
</head>
<body>
<div id="form-div">
<form id="form1" action="/users/login" method="post">
<p>用户名:<input name="userName" type="text" id="txtUserName" tabindex="1" size="15" value=""/></p>
<p>密 码:<input name="password" type="password" id="TextBox2" tabindex="2" size="16" value=""/></p>
<p><input type="submit" value="登录"> <input type="reset" value="重置"></p>
</form>
</div>
</body>
</html>
ajax实现form提交方式
注意事项<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login test</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="ajax方式">
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
function login() {
$.ajax({
//几个参数需要注意一下
type: "POST",//方法类型
dataType: "json",//预期服务器返回的数据类型
url: "/users/login" ,//url
data: $('#form1').serialize(),
success: function (result) {
console.log(result);//打印服务端返回的数据(调试用)
if (result.resultCode == 200) {
alert("SUCCESS");
}
;
},
error : function() {
alert("异常!");
}
});
}
</script>
</head>
<body>
<div id="form-div">
<form id="form1" onsubmit="return false" action="##" method="post">
<p>用户名:<input name="userName" type="text" id="txtUserName" tabindex="1" size="15" value=""/></p>
<p>密 码:<input name="password" type="password" id="TextBox2" tabindex="2" size="16" value=""/></p>
<p><input type="button" value="登录" onclick="login()"> <input type="reset" value="重置"></p>
</form>
</div>
</body>
</html>
<form id="mainForm" method="post" enctype="multipart/form-data" class="jsrz_main_information">
<input type="text" name="UserName" value="" />
<div class="jsrz_main_button">
<input type="submit" value="提交" id="agreementSub">
</div>
</form>
$("#agreementSub").on("click",function(){
$('#mainForm').ajaxSubmit( //ajax方式提交表单
{
url: '/personal/kaike',
type: 'post',
dataType: 'json',
beforeSubmit: function () {},
success: function (data) {
if (data.Res == "True" || data.Res == true) {
$('.jsrz_main_check').html('您的申请已提交,我们将会在1-2个工作日内进行审核,请耐心等待!');
} else {
alert(data.Msg);
}
},
clearForm: false,//禁止清楚表单
resetForm: false //禁止重置表单
});
});
三、不使用jQuery.from表单插件的方式$('#mainForm').ajaxSubmit()//中的代码部分也可以封装为一个方法,在其他地方调用。
$("#maniForm").submit(function (envent)
{
envent.preventDefault();
var form = $(this);
$.ajax({
url: form.attr("action"),
type: form.attr("mathod"),
data: form.serialize(),
dataType: "json",
beforeSend: function ()
{
$("#ajax-loader").show();
},
error: function ()
{
},
complete:function () {
$("#ajax-loader").hide();
},
success: function (data)
{
$("#article").html(data);
}
});
});
以上是html代码,为了方便大家copy,css直接在标签里了;<form id = "form_insert" method = "post">
<table style = "font-size: 13px; margin: 13px auto;">
<tr>
<td style = "text-align: right;">类型</td>
<td>: <input id = "acttype" style = "width:150px" class = "easyui-textbox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">名称</td>
<td>: <input id = "actname" style = "width:150px" class = "easyui-textbox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">开始时间</td>
<td>: <input id = "actstarttime" style = "width:150px" class = "easyui-datetimebox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">结束时间</td>
<td>: <input id = "actendtime" style = "width:150px" class = "easyui-datetimebox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">省</td>
<td>: <input id ="mem_Province" style = "width:150px" class = "easyui-combobox" data-options = "required:true"></td>
</tr>
<tr><td colspan="2" style="height: 13px"></td>
</tr>
<tr>
<td style="text-align: right;">市</td>
<td>: <input id = "mem_City" style = "width:150px" class = "easyui-combobox" data-options = "required:true"></td>
</tr>
<tr><td colspan = "2" style = "height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">门店</td>
<td>: <input id = "mem_Shop" style = "width:150px" class = "easyui-combobox" data-options = "required:true"></td>
</tr>
<tr><td colspan="2" style="height: 13px"></td>
</tr>
<tr>
<td style = "text-align: right;">具体地址</td>
<td>: <input id = "actadd" style = "width:150px" class = "easyui-textbox" data-options = "required:true"></td>
</tr>
</table>
</form>
<form id = "form_sub" style = "font-size: 13px;">
<table style="font-size: 13px; margin: 13px auto;">
<tr>
<td style = "text-align: right;">上传图片</td>
<td>: <input class = "easyui-filebox" name = 'photo' style = "width:153px" data-options = "required:true,prompt:'选择上传图片',buttonText:' 选 择 '"></td>
<td><input type = 'text' id = "Item" name = 'item' style = "display:none;"></td>
</tr>
</table>
</form>
<p style = "text-align:right; padding:2px 5px;">
<a id = "sub" class = "easyui-linkbutton" data-options = "iconCls:'icon-ok'" href = "javascript:void(0)">
保存
</a>
<a class = "easyui-linkbutton" data-options = "iconCls:'icon-quxiao'" href = "javascript:void(0)" onclick = "window_open($('#insert_form'), 'close')">
取消
</a>
</p>
大家看到了我用了FormData方法,说真的这个在html5里实在是太好用了,上传图片都不用再写enctype = 'multipart/form-data';$( '#sub' ).click( function () {
var actTimeStart1 = $ ('#actstarttime') . datebox ('getValue');
var actTimeStart = changeDateToLong(actTimeStart1);
var actTimeEnd1 = $('#actendtime').datebox('getValue');
var actTimeEnd = changeDateToLong(actTimeEnd1);
if(actTimeStart != '' && actTimeEnd != '' && (actTimeStart - actTimeEnd > 0)){
$.messager.alert('警告','结束时间不能小于开始时间!','error');
return false;
}
else{
if ($('#form_insert').form('validate')) {
var actType = document.getElementById("acttype").value;
var actName = document.getElementById("actname").value;
var actArea = document.getElementById("actadd").value;
var actTimeStart1 = $('#actstarttime').datebox('getValue');
var actTimeStart = changeDateToLong(actTimeStart1);
var actTimeEnd1 = $('#actendtime').datebox('getValue');
var actTimeEnd = changeDateToLong(actTimeEnd1);
var t2 = $('#mem_Shop').combobox('getValue');
var jsonObj = {actType:actType,actName:actName,actTimeStart:actTimeStart,actTimeEnd:actTimeEnd,actArea:actArea,t2:t2};
var activityMemberJson = JSON.stringify(jsonObj);
document.getElementById("Item").value=activityMemberJson;
var form = new FormData(document.getElementById("form_sub"));
$.ajax({
url : ../activity/actionActivityInsert', //<a rel="nofollow" href="http://www.cnblogs.com/jayxxxxxxx/" target="_blank">http://www.cnblogs.com/jayxxxxxxx/</a>
type : "post",
data : form, //第二个Form表单的内容
processData : false,
contentType : false,
error : function(request) {
},
success : function(data) {
$('#box').datagrid('reload');
}
});
window_open($('#insert_form'), 'close');
}else {
$.messager.alert('警告' , '信息不完整!' , 'error');
}
}
});
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛