简体   繁体   English

联系表格 7 将禁用的属性添加到按钮以防止重复提交

[英]Contact form 7 add disabled attr to button to prevent double submission

I have a WordPress site and there is Contact form 7 plugin and I want to add attr to submit button to disable double submission.我有一个 WordPress 网站,并且有 Contact form 7 插件,我想将 attr 添加到提交按钮以禁用双重提交。 Now I have this code to prevent double submission现在我有这个代码来防止双重提交

$(document).on('click', '.wpcf7-submit', function(e){
            if( $('.ajax-loader').hasClass('is-active') ) {
                e.preventDefault();
                return false;
            }
        });

but I want to add attr disabled while form sending or getting error response for better user experience但我想在表单发送或获取错误响应时添加 attr disabled 以获得更好的用户体验

Improving on Matt's answer -改进马特的答案 -

$('.wpcf7-form').on('submit', function() {
   $(this).find('.wpcf7-submit').attr('disabled', true);
});

This would disable the submit button when clicked on it.单击它时,这将禁用提交按钮。 Now to get that activated again after success or failure you would need to remove the attribute after the submission is complete(whether success or failure).现在要在成功或失败后再次激活该属性,您需要在提交完成后删除该属性(无论是成功还是失败)。 Since the plugin developer is a bit whimsical about how the events work, I am writing this solution for first quarter of 2019 -由于插件开发人员对事件的工作方式有点异想天开,我正在为 2019 年第一季度编写此解决方案 -

$('.wpcf7').on('wpcf7submit', function (e) {
   $(this).find('.wpcf7-submit').removeAttr('disabled');
});

where '.wpcf7' is the parent container of the form, '.wpcf7-form' is the form itself.其中“.wpcf7”是表单的父容器,“.wpcf7-form”是表单本身。 The ' wpcf7submit ' is event listener that the DOM listens to, after the form gets submitted(irrespective of the fact that is valid or invalid). ' wpcf7submit ' 是 DOM 在提交表单后监听的事件监听器(不管是有效还是无效)。

This will disable the button and submit the form.这将禁用按钮并提交表单。 You need to re-call submit after disabling the button.您需要在禁用按钮后重新调用提交。

jQuery( '.wpcf7-submit' ).click(function() {
    jQuery( this ).attr( 'disabled', true );
    jQuery( this ).submit();
});

This will re-enable the button if there's an error with the submission.如果提交出现错误,这将重新启用按钮。

document.addEventListener( 'wpcf7invalid', function() {
    jQuery( '.wpcf7-submit' ).attr( 'disabled', false );
}, false );
$(document).on('click', '.wpcf7-submit', function(e){
    $(this).prop('disabled',true);
});

I implemented this script to help prevent multiple submissions.我实现了这个脚本来帮助防止多次提交。 The biggest difference from the others is that it works with multiple CF7 forms on each page.与其他人最大的不同是它可以在每个页面上使用多个 CF7 表单。 Basically, it disables the form and the submit button on submit (since a form can also be submitted with an Enter-press), adds a new label "Please Wait.." to the submit button, and re-enables them if there are input errors.基本上,它在提交时禁用表单和提交按钮(因为也可以使用 Enter-press 提交表单),向提交按钮添加一个新标签“请稍候..”,如果有则重新启用它们输入错误。 Also, not dependent on jQuery (Vanilla JS).此外,不依赖于 jQuery(Vanilla JS)。

(function () {
  var elems = document.querySelectorAll('.wpcf7');
  if (!elems.length) {
    return false;
  }
  var forms = document.querySelectorAll('.wpcf7-form');
  if (!forms.length) {
    return false;
  }

  function _evtFormSubmit() {
    this.disabled = true;
    var submitBtn = this.querySelector('button[type="submit"]');
    submitBtn.disabled = true;
    submitBtn.setAttribute('data-default-text', submitBtn.innerText);
    submitBtn.innerHTML = '<span>Please wait...</span>';
  }

  function _evtInvalid(e) {
    var thisForm = document.querySelector('#' + e.detail.id + ' form');
    thisForm.disabled = false;
    var submitBtn = thisForm.querySelector('button[type="submit"]');
    setTimeout(function() {
      submitBtn.disabled = false;
    submitBtn.innerHTML = '<span>' + submitBtn.getAttribute('data-default-text') + '</span>';
    }, 600); // give it a bit of time in case it is a fast submit
  }

  for(var i = forms.length-1; i >= 0; i--) {
    forms[i].addEventListener('submit', _evtFormSubmit, false);
  }
  for(i = elems.length-1; i >= 0; i--) {
    elems[i].addEventListener('wpcf7invalid', _evtInvalid, false);
  }

})();

Note: if you have more than one submit button (why?), this only affects the first button in the form.注意:如果您有多个提交按钮(为什么?),这只影响表单中的第一个按钮。

For future people who are looking for a solution here.对于正在这里寻找解决方案的未来人。 Simple SCSS/CSS option without Javascript need.无需 Javascript 的简单 SCSS/CSS 选项。 For me is work pefect.对我来说是完美的工作。 It always works reliably for me.它对我来说总是可靠的。 (2022) (2022)

.wpcf7-form {
    &.submitting {
        .wpcf7-submit {
            pointer-events: none;
        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM