之前做了个ajax提交表单的问题,使用了FormData,但是提交会直接进入error方法块,后听以前同学所说,将
form表单
js代码
$(function(){
$("#submit_btn").on("click",function(){
submit();
});
});
function submit(){
var formData = new FormData($("#upForm")[0]);
var appType = $("#appType").val();
if(!/[0-9]+/.test(appType)){
alert('appType must be number')
}
$.ajax({
type:'post',
url:$("#upForm").attr('basePath')+'version/upload',
cache:false,
contentType:false,
processData:false,
data:formData,
dataType : 'json',
success:function(callback){
$("#msg_p").text(callback.msg);
$("#msg_p").show();
setTimeout(function(){
$("#msg_p").hide();
if (callback.success == true)
alert(1);
//window.location.href="version/upPage";
else
alert(0);
},500);
},
error:function(){
alert("进入error function");
}
});
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
以下两种标签会自动提交form表单:
<button>
<input type="submit">
以下标签不会自动提交form表单:
<input type="button">
如果你是用前面那两种,浏览器本身帮你submit一次,你的代码又submit了一次$("#submit_btn").on("click",function(){
就重复了。
不要把
<button>标签当成<form>中的input元素。用button指定type=button就能避免