JQuery 从提交函数内部提交表单
以下是我想在 JQuery 脚本中执行的操作。在下面的提交函数(第 4 个)中,我想确定表单是否有文件输入并使用 ajax 提交或只是不使用 ajax 的常规表单提交。换句话说,如果表单已上传,则进行常规提交。
我在下面的提交功能中写了这个问题。这是我让它发挥作用唯一需要的东西。
谢谢你!
function FindFileInput(){
// check for file input
var FileInput = $('input:file');
if(FileInput.length > 0){
return true;
}else{
return false;
}
}
function validation(){
// code to validate form
...
}
function ajaxSubmit(formData){
$.ajax({
// ajax submit code
});
}
$(myForm).submit(function(e){
e.preventDefault();
// 1. if NO file input present
if(FindFileInput() === false){
if(validation() === true){
// serialize and call ajaxSubmit function
}
}
// 2. if file input IS present
if(FindFileInput() === true){
if(validation() === true){
// QUESTION: How do I submit the form here???
}
}
});
The following is what I'd like to do in my JQuery script. In the submit function (4th) below, I want to decide if the form has file input and submit with either ajax or just a regular form submit without ajax. In other words, if the form has upload, do a regular submit.
I wrote the question in the submit function below. That is the only thing I need to make it work.
Thank you!
function FindFileInput(){
// check for file input
var FileInput = $('input:file');
if(FileInput.length > 0){
return true;
}else{
return false;
}
}
function validation(){
// code to validate form
...
}
function ajaxSubmit(formData){
$.ajax({
// ajax submit code
});
}
$(myForm).submit(function(e){
e.preventDefault();
// 1. if NO file input present
if(FindFileInput() === false){
if(validation() === true){
// serialize and call ajaxSubmit function
}
}
// 2. if file input IS present
if(FindFileInput() === true){
if(validation() === true){
// QUESTION: How do I submit the form here???
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 http://api.jquery.com/submit/:
所以改变你的逻辑。不要将 e.preventDefault() 作为默认调用,然后尝试撤消它,而是仅在实际需要时才调用它。
From http://api.jquery.com/submit/:
So turn your logic around. Don't call e.preventDefault() as default and then try to undo it, but rather only call it when it is actually needed.
不要这么早调用
e.preventDefault()
,仅当您确实想要阻止以默认方式发布表单时(即选择文件时)才执行此操作。这样你就可以去掉第二个 if 语句,当有文件要发送时,让 Submit JS 函数不执行任何操作。这样,如果未选择文件,表单将以默认方式发送。Don't call the
e.preventDefault()
so early, do it only when you actually want to stop the form from being posted the default way (thus, when a file is selected). That way you can get rid of the second if-statement, and just let the submit JS-function do nothing when there is a file to send. That way the form will be sent the default way, if no file is selected.this.submit()
前提是您没有使用demo:
http://jsfiddle.net/4buHP/
本机
.submit()
不会触发 jQuery 事件。this.submit()
provided that you haven't overridden it with something like<input name="submit" />
demo:
http://jsfiddle.net/4buHP/
The native
.submit()
doesn't fire the jQuery event.如果您希望继续提交,请不要阻止默认行为并返回 true。
If you want the submit to continue just don't prevent the default behavior and return true.
简单的!我删除了 e.preventDefault();并添加了一些其他条件。然而,我提供了一个非常程序化(不太理想)的解决方案。
Simple! I removed e.preventDefault(); and added some else conditions. However I provided a very procedural (less ideal) solution.