jQuery '.click(callback)' 阻止默认事件

jQuery '.click(callback)' prevents the default event

本文关键字:默认 事件 click jQuery callback      更新时间:2023-09-26

我试图阻止双击表单按钮,禁用该元素几秒钟。

<form action="#" method="POST">
    <input type="text" />
    <input type="password" />
    <button class="myButton" type="submit">Send</button>
</form>

var that = this;
this.element = $(".myButton");
this.element.click(function(e) {
    this.element.prop("disabled", true);
    window.setTimeout(function() {
        that.element.prop("disabled", false);
    }, 2000);
});

它成功地启用/禁用,但使用.click()功能可以防止(自动?O_o) 事件的传播,并且页面不遵循表单操作属性中的链接。

请注意,我正在尝试将行为添加到以前工作正常的表单按钮(发布到aciton链接)。此外,启用/禁用工作得很好(无论我在改编上面的代码时可能犯的最终错误)。

PS - 任何解决方案都必须是跨浏览器的,并且与IE8兼容。

通过禁用该按钮,您将阻止发布操作发生。因此,您要做的是禁用表单,然后使用javascript调用实际提交表单。 这可能看起来像这样:

$('.myButton').click(function() {
    $button = $(this);
    $button.attr('disabled', true);
    $button.closest('form').submit();
    setTimeout(function() {
        $button.attr('disabled', false);
    }, 2000);
});