简体   繁体   English

禁用表单提交,直到使用jQuery验证字段

[英]Disable Form Submit until Fields have been validated using jQuery

I have modified this script http://jsfiddle.net/mblase75/xtPhk/1/ to submit a form. 我已修改此脚本http://jsfiddle.net/mblase75/xtPhk/1/以提交表单。 The goal is to disable submit button until all form fields have been validated. 目标是在所有表单字段都经过验证之前禁用“提交”按钮。 This works fine the first time but since, my form does not have a action jquery handles the submit as well. 这在第一次工作正常但是因为,我的表单没有动作jquery也处理提交。

So after I submit the form, the submit button does not get disabled. 因此,在我提交表单后,提交按钮不会被禁用。 I have to refresh the page in order for it to get disabled. 我必须刷新页面才能禁用它。

What I am trying to do is, after every post.. the submit button should get disabled, without refreshing the page. 我想要做的是,在每个帖子之后......提交按钮应该被禁用,而不刷新页面。

Is this possible ? 这可能吗 ?

It does work if my form has a action page. 如果我的表单有一个操作页面,它确实有效。 but I dont want that 但我不想要那个

Form Submit: 表格提交:

       $(document).ready(function() {
    $("#paForm").submit(sendForm)
    });

   function sendForm() {
      $.post('pa_submit.cfm',$("#paForm").serialize(),function(data,status){
    $("#result").html(data)
});// end of submit 
$( '#paForm' ).each(function(){
      this.reset(); // end of reset 
  });
return false
  }

Disable Submit Button until all fields have been validated 禁用“提交”按钮,直到验证所有字段

       $(document).ready(function() {
       $form = $('#paForm'); // cache
       $form.find(':input[type="submit"]').prop('disabled', true); // disable submit btn
      $form.find(':input').change(function() { // monitor all inputs for changes
    var disable = false;
       $form.find(':input').not('[type="submit"]').each(function(i, el) { // test all inputs for values
            if ($.trim(el.value) === '') {
            disable = true; // disable submit if any of them are still blank
           }
        });
         $form.find(':input[type="submit"]').prop('disabled',disable);
      });
    });

I am using a jquery function to post my values to a database. 我正在使用jquery函数将我的值发布到数据库。 My form does not have a action. 我的表格没有动作。

http://jsfiddle.net/bC6GF/ http://jsfiddle.net/bC6GF/

Here is the jsfiddle page, which shows my issue. 这是jsfiddle页面,它显示了我的问题。

Why not disable the button after submit? 为什么不在提交后禁用按钮?

You already have the submission function in place: 您已经有提交功能:

function sendForm() {
 $.post('pa_submit.cfm',$("#paForm").serialize(),function(data,status){
  $("#result").html(data)
 });// end of submit 
 $( '#paForm' ).each(function(){
  this.reset(); // end of reset 
 });
 return false;
}

Extend with a call to the submit button to disable it: 通过调用提交按钮进行扩展以禁用它:

function sendForm() {
 $.post('pa_submit.cfm',$("#paForm").serialize(),function(data,status){
  $("#result").html(data)
 });// end of submit 
 $( '#paForm' ).each(function(){
  this.reset(); // end of reset 
 });
 $("#paForm").find(':input[type="submit"]').prop('disabled', true); 
 return false;
}

That should disable the button after each submit. 这应该在每次提交后禁用按钮。

Unrelated but something you may want to look into are the jQuery JavaScript Style Guides and cleanup some of your code. 不相关但你可能想要研究的是jQuery JavaScript样式指南和清理你的一些代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 禁用提交按钮,直到验证所有表单字段 - Disable Submit button until all form fields are validated Javascript:如何在所有字段都经过验证之前禁用提交按钮? - Javascript: How to disable submit button until all fields are validated? 使用Redux表单,如果未填写任何字段,如何禁用提交按钮? - Using Redux Form, how can I disable the submit button if no fields have been filled out? 禁用表单提交按钮,直到填充所有输入/文本区域字段,而不使用 for 循环(无 jQuery) - Disable form submit button until all input/textarea fields are filled, without using for loop (no jQuery) bootstrap 4验证禁用提交按钮,直到表单验证 - bootstrap 4 validation disable submit button until form validated 提交包含已使用jQuery移动的列表框项目的隐藏字段 - Submit hidden fields with listbox items that have been moved using jQuery 禁用提交按钮,直到在数据库上验证字段 - Disable submit button until field is validated on database 禁用jQuery的提交按钮,直到所有字段都具有值(输入,单选,选择,复选框) - Disable submit button with jQuery until all fields have values (input, radio, select, checkbox) 禁用/启用提交按钮,直到填写完所有表格 - Disable/Enable Submit Button until all forms have been filled 禁用表单提交按钮,直到已填充文本区域? - disable a form submit button until a textarea has been populated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM