是否使用绑定功能禁用按钮



你好,我如何禁用带有绑定功能的按钮10秒?

jQuery('#wsf-1-field-155').bind('click', function() {
ScanRegistration();
setTimeout(function() {
jQuery('#wsf-1-field-155').bind();
}, 10000);
})

我用这个解决了这个问题,我用.removeAttr更改了.removeProp

jQuery('#wsf-1-field-155').on('click', function() {
jQuery(this).prop('disabled', true);
ScanRegistration();
setTimeout(() =>
jQuery(this).removeAttr('disabled'), 20000);
})

这里有一个简单的JavaScript解决方案。scanRegistration()只计数10秒。

示例中对详细信息进行了注释

// count and interval variables should be declared outside of function
let i = 0;
let int;
// Reference the <button>
const btn = document.querySelector('#GO');
// Function enables the <button>
const enableBtn = () => btn.disabled = false;
/*
Bind the "click" event to the <button>
Disable <button>
call scanRegistration()
call enableBtn() @10 seconds
*/
btn.onclick = function(event) {
this.disabled = true;
scanRegistration();
setTimeout(() => {
enableBtn();
}, 10000);
};
// Function logs every second
const logScan = i => console.log("SCAN: " + i);
/*
Set an interval at the rate of 1 second
Increment the count variable i
call logScan()
If i is equal to or more than 10 end interval
*/
function scanRegistration() {
console.log("START SCAN");
int = setInterval(() => {
i++;
logScan(i);
if (i >= 10) {
console.log("END SCAN");
clearInterval(int);
}
}, 1000);
}
<button id="GO">GO!</button>

.bind()已弃用。您应该使用.on()

您不使用事件绑定来禁用按钮,而是设置其disabled属性。然后使用removeAttr()在10秒后重新启用。

jQuery('#wsf-1-field-155').on('click', function() {
$(this).attr('disabled', true);
ScanRegistration();
setTimeout(() =>
$(this).removeAttr('disabled'), 10000);
})

最新更新