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

Kam*_*her 1 javascript forms jquery javascript-events event-propagation

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

<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);
});
Run Code Online (Sandbox Code Playgroud)

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

注意我正在尝试将行为添加到以前完美运行的表单按钮(POST到 aciton链接).此外,启用/禁用工作也很完美(无论我在上述代码的调整中可能出现的最终错误).

PS - 任何解决方案必须是跨浏览器并与IE8兼容.

Mik*_*ant 5

通过禁用该按钮,您可以防止发布后期操作.所以你想要做的是禁用表单,然后使用javascript调用实际提交表单.这看起来像这样:

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