本文介绍了jQuery的上提交preventDefault()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个提交按钮,输入HTML表单='文件'控制:
I have html form with submit button and input='file' control:
<form action="/Profile/UploadFile" enctype="multipart/form-data" method="post" onsubmit="OnUploadSubmit(this)">
OnUploadSubmit功能如下:
OnUploadSubmit function looks like this:
if (e.files[0].size > 5000000) {
$('#upload_error').css('display', 'block');
$('#upload_error').text('The file size is too large for upload')
e.preventDefault();
return false;
}
var files = e.files;
var ext = $('#file_uploader').val().split('.').pop().toLowerCase();
if ($.inArray(ext, ['jpeg', 'jpg', 'png']) == -1) {
$('#upload_error').css('display', 'block');
$('#upload_error').text('Only, jpg, jpeg, png is allowed');
e.preventDefault();
return false;
}
return true;
}
电子preventDefault()和返回false。不列入工作形式反正submiting。
e.preventDefault() and return false; doesnot working form is submiting anyway.
有谁知道什么是我的问题?
Does anybody knows what is my problem?
感谢
推荐答案
首先你不能以这种方式使用preventDefault是因为你没有通过事件对象为功能,而旁边:
first you can't use preventDefault in this way because you not passed event object into function, and next:
onsubmit="return OnUploadSubmit(this)"
我建议:
添加id属性,并删除的onsubmit:
add id attribute and remove onsubmit:
<form id="upload" action="/Profile/UploadFile" enctype="multipart/form-data" method="post">
jQuery的:
jQuery:
$(function(){ // Document Ready
$('#upload').submit(function(e){
e.preventDefault();
if (e.files[0].size > 5000000) {
$('#upload_error').css('display', 'block');
$('#upload_error').text('The file size is too large for upload')
}
var files = e.files;
var ext = $('#file_uploader').val().split('.').pop().toLowerCase();
if ($.inArray(ext, ['jpeg', 'jpg', 'png']) == -1) {
$('#upload_error').css('display', 'block');
$('#upload_error').text('Only, jpg, jpeg, png is allowed');
}
}
});
});
这篇关于jQuery的上提交preventDefault()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!