3

我的问题是在单击任何表单提交按钮后,使用 jQuery 实现禁用页面上按钮的最佳方法是什么。这里有类似的东西是在表单同步提交之后执行的,其中声明我的代码有问题(1.)“可能”运行。所有代码都已准备好文档。

  1. 我在我们的项目中看到过这样的代码,但它真的可以保证最后一行 - button("disable") 被调用吗?

    $("#saveButton").on("click", function () {
        $("#myForm").submit();
        $("#saveButton").button("disable");
    });
    
  2. 替代方案是这样(如果按钮被禁用,这可能会出现问题,但最后一行也可能失败,所以它并不真正“感觉良好”。

    $("#saveButton").on("click", function () {
        $("#saveButton").button("disable");
        $("#myForm").submit();
    });   
    
  3. 这可能是最好的方法,对吧?会不会有“陷阱”?

    $("#myForm").submit(function () {
        $("#saveButton").button("disable");
        // disable also all the elements that can do a submit
        return true;
    });
    
4

1 回答 1

1

提交几乎是最好的方法,“一次”方法只会给你一次机会,如果你的提交失败或没有验证你被卡住了

$('#myForm').on('submit',function () {
    $('#saveButton').button('disable');
    // disable also all the elements that can do a submit

    //Validate
    if (validate)
        return true;
    else  {
         //cancels submit
         $('#saveButton').button('enable');
         return false;
    }
});
于 2013-08-28T13:06:47.600 回答