转载自:https://blog.csdn.net/qq_15204179/article/details/82144522
表单:
-
<form class="" id="handle-form">
-
<input type="text" name="id" id="id" value="">
-
<input type="text" id="operator" name="operator" >
-
</form>
第一种获取form中数据的方法:
new FormData($('#uploadForm')[0])用法与$("#handle-form").serialize()差不多,就是 可以上传文件但是对于jquery的要求是, 版本1.8及其以上方可支持;
注意:按钮type非submit,而是buttern,Action 为空或无;
var handle-form = $("#handle-form").serialize();
-
$.ajax({
-
url: "",
-
type: "post",
-
contentType: "application/json; charset=utf-8",
-
data: $("#handle-form").serialize();,
-
dataType: "json",
-
success: function (data) {
-
-
}
第二种获取form中数据的方法:
注意:按钮type非submit,而是buttern,Action 为空或无;
-
var formSerial = {};
-
$($("#handle-form").serializeArray()).each(function(){
-
formSerial[this.name] = this.value;
-
});
-
-
var fromValue = JSON.stringify(formSerial)
-
$.ajax({
-
url: "",
-
type: "post",
-
contentType: "application/json; charset=utf-8",
-
data: JSON.stringify(formSerial),
-
dataType: "json",
-
success: function (data) {
-
-
}
form表单提交的几种方法:
一.表单提交
-
<form action=’/login’ method=’post’ id = "loginForm">
-
-
<input type=’text’ name=’username’ />
-
-
<input type=’password’ name=’password’/>
-
-
<input type=’submit’ value=’登陆'/>
-
-
</form>
二.Ajax提交form表单
-
$('#loginForm').submitForm({
-
url: "/login",
-
dataType: "text",
-
callback: function (data) {
-
-
}
-
},
-
before: function () {
-
-
}
-
}).submit();
三.form表单提交附件
需要设定form的enctype="multipart/form-data"并且添加<input type=’file’>
-
//jQuery提交
-
$("#jqueryBtn").click(function(){
-
$("#loginForm").submit();
-
})
-
//js提交
-
$("#jsBtn").click(function(){
-
document.loginForm.action="RegisterAction.action";
-
document.loginForm.submit();
-
-
})
-
-
//js提交
-
$("#jsBtn").click(function(){
-
document.getElementById('').submit();
-
})
-
-
//ajax提交
-
$("#ajaxBtn").click(function() {
-
var params = $("#loginForm").serialize();
-
$.ajax( {
-
type : "POST",
-
url : "RegisterAction.action",
-
data : params,
-
success : function(msg) {
-
alert("success: " + msg);
-
}
-
});
-
})