JQuery 从提交函数内部提交表单

发布于 2024-12-28 12:15:52 字数 902 浏览 0 评论 0原文

以下是我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里寻她 2025-01-04 12:15:52

来自 http://api.jquery.com/submit/

现在,当提交表单时,会发出警报消息。发生这种情况
在实际提交之前,因此我们可以通过以下方式取消提交操作
在事件对象上调用 .preventDefault() 或返回 false
来自我们的处理程序。当另一个事件发生时,我们可以手动触发该事件
单击元素:

所以改变你的逻辑。不要将 e.preventDefault() 作为默认调用,然后尝试撤消它,而是仅在实际需要时才调用它。

$(myForm).submit(function(e){

    // 1. if NO file input present
    if(FindFileInput() === false){
        if(validation() === true){
            ajaxSubmit(formdata);
        }
     }

     // 2. if file input IS present
     if(FindFileInput() === true){
         if(validation() === true){
             return true; // submit form as normal, don't call e.preventDefault()
         }
     }

     // Prevent form from submitting normally
     e.preventDefault();
     return false;
});

From http://api.jquery.com/submit/:

Now when the form is submitted, the message is alerted. This happens
prior to the actual submission, so we can cancel the submit action by
calling .preventDefault() on the event object or by returning false
from our handler. We can trigger the event manually when another
element is clicked:

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.

$(myForm).submit(function(e){

    // 1. if NO file input present
    if(FindFileInput() === false){
        if(validation() === true){
            ajaxSubmit(formdata);
        }
     }

     // 2. if file input IS present
     if(FindFileInput() === true){
         if(validation() === true){
             return true; // submit form as normal, don't call e.preventDefault()
         }
     }

     // Prevent form from submitting normally
     e.preventDefault();
     return false;
});
梦归所梦 2025-01-04 12:15:52

不要这么早调用 e.preventDefault(),仅当您确实想要阻止以默认方式发布表单时(即选择文件时)才执行此操作。这样你就可以去掉第二个 if 语句,当有文件要发送时,让 Submit JS 函数不执行任何操作。这样,如果未选择文件,表单将以默认方式发送。

$(myForm).submit(function(e){

   // 1. if NO file input present
   if(FindFileInput() === false){
      if(validation() === true){
         e.preventDefault();
         // Do your AJAX-stuff here
      }
   }

});

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.

$(myForm).submit(function(e){

   // 1. if NO file input present
   if(FindFileInput() === false){
      if(validation() === true){
         e.preventDefault();
         // Do your AJAX-stuff here
      }
   }

});
享受孤独 2025-01-04 12:15:52

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.

微凉徒眸意 2025-01-04 12:15:52

如果您希望继续提交,请不要​​阻止默认行为并返回 true。

$(myForm).submit(function(e){

    if(!FindFileInput()){
        if(validation()){
            //AJAX method
        }
    }else{
        if(validation()){
            return true;
        }
    }
    e.preventDefault();
    return false;
});

If you want the submit to continue just don't prevent the default behavior and return true.

$(myForm).submit(function(e){

    if(!FindFileInput()){
        if(validation()){
            //AJAX method
        }
    }else{
        if(validation()){
            return true;
        }
    }
    e.preventDefault();
    return false;
});
揽月 2025-01-04 12:15:52

简单的!我删除了 e.preventDefault();并添加了一些其他条件。然而,我提供了一个非常程序化(不太理想)的解决方案。

$(myForm).submit(function(e){

   // 1. if NO file input present
   if(FindFileInput() === false){
      if(validation() === true){
         // serialize and call ajaxSubmit function

         return false; // stops form submissions, same as preventDefault but less chars.
      }
   }

   // 2. if file input IS present
   if(FindFileInput() === true){
      if(validation() === true){
         // QUESTION: How do I submit the form here???
         // no return false, moves forward.
      } else {
          return false;
      }
   } else {
      return false;
   }
});

Simple! I removed e.preventDefault(); and added some else conditions. However I provided a very procedural (less ideal) solution.

$(myForm).submit(function(e){

   // 1. if NO file input present
   if(FindFileInput() === false){
      if(validation() === true){
         // serialize and call ajaxSubmit function

         return false; // stops form submissions, same as preventDefault but less chars.
      }
   }

   // 2. if file input IS present
   if(FindFileInput() === true){
      if(validation() === true){
         // QUESTION: How do I submit the form here???
         // no return false, moves forward.
      } else {
          return false;
      }
   } else {
      return false;
   }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文