简体   繁体   English

禁用表单提交按钮,不阻止 HTML5 验证

[英]Disable button on form submit, without preventing HTML5 validation

Hoping people can help as I'm pulling out my hair.希望大家在我拔头发时能提供帮助。 I need to disable the button after clicked because some of my lovely users love spam-clicking the buttons:我需要在点击后禁用按钮,因为我的一些可爱的用户喜欢点击按钮:

在此处输入图片说明

At the moment, if they try to submit without completing a field, the form displays the HTML5 validation errors (both fields are required).目前,如果他们尝试在未完成字段的情况下提交,表单将显示 HTML5 验证错误(两个字段都是必需的)。 However anything I've implemented, onclick, onsubmit, everything that uses JS to disable the button means that the HTML validation no longer runs.但是,我实现的任何内容,onclick,onsubmit,使用 JS 禁用按钮的所有内容都意味着 HTML 验证不再运行。 I've tried it on the form and on the button, using on event function calls, and with event listeners.我已经在表单和按钮上尝试过它,使用事件函数调用和事件侦听器。 I'm stumped.我难住了。

I know I can complete validation with JS, but I don't really want to have to if I can use HTML5;我知道我可以用 JS 完成验证,但如果我可以使用 HTML5,我真的不想这样做; what would be the point duplicating existing functionality...?复制现有功能有什么意义......? Is this possible, or do I need to add the validation from a JS perspective?这是可能的,还是我需要从 JS 的角度添加验证?

After a little while I'm then wanting to un-disable the button to give them chance to respond to the validation errors (if applicable), otherwise by then the form should have submitted and the server is busy chewing on post data.过了一会儿,我想取消禁用按钮,让他们有机会响应验证错误(如果适用),否则到那时表单应该已经提交并且服务器正忙于处理发布数据。 Something like:就像是:

$("#submit_button").prop('disabled', true)
setTimeout(() => {
    $("#submit_button").prop('disabled', false)
}, 2000)

Edit: I have been informed that by disabling the button, it's disabling the ability to do the html5 validation.编辑:我被告知,通过禁用按钮,它禁用了进行 html5 验证的能力。 So how does one stop users clicking the button multiple times without disabling it?那么如何阻止用户多次点击按钮而不禁用它呢?

Instead of disableing the button, you could turn off the pointer events:您可以关闭指针事件,而不是禁用按钮:

$("#submit_button").style.pointerEvents = "none";

or better, add a class to the button, which change the color, so it looks disabled (put an opacity on it) and add the pointerEvents property.或者更好的是,向按钮添加一个类,它会更改颜色,使其看起来被禁用(在其上放置不透明度)并添加 pointerEvents 属性。

#submit_button.disabled{
  opacity:.5;
  pointerEvents:none;
}

The answer was alluded to by @Raqha above, but the code was slightly off.上面的@Raqha 暗示了答案,但代码略有偏差。 Indeed temporarily turning off the pointer was the way to go.事实上,暂时关闭指针是要走的路。 Thanks!谢谢!

So this code:所以这段代码:

$('#submit_button').click(e => {
    $("#submit_button").addClass('disabled')
    console.log($('#' + e.target.id).parent('form').children('input'))
    setTimeout(() => {
        $("#submit_button").removeClass('disabled')
        console.log('UN-DISABLED')
    }, 2000)
})

With this CSS:使用这个 CSS:

button.disabled {
    pointer-events: none;
}

button.enabled {
    pointer-events: all;
}

Obviously you can change the button text or add a spinner as necessary while disabled to suggest submitting if desired.显然,您可以根据需要更改按钮文本或添加微调器,同时禁用以根据需要建议提交。 Thanks internet peeps 😺感谢互联网的窥视😺

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

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