【问题标题】:jquery prop.(“disabled”,true) not working, niether is .removeAttr('disabled'); [duplicate]jquery prop.(“disabled”,true) 不工作,也不是 .removeAttr('disabled'); [复制]
【发布时间】:2019-10-31 10:42:19
【问题描述】:

出于某种原因,当我尝试通过运行重新启用按钮时:

$("#reloadSuggestions").removeAttr('disabled');

我也试过了:

$("#reloadSuggestions").prop('disabled', false);

我尝试的任何操作都不会删除 disabled 属性,它会将其留空。在控制台中,如果我运行 $("#reloadSuggestions").removeAttr('disabled'); 它会删除它,但不是在我的实际代码运行时

PS 重新回答这个问题,因为它是 closed as off-topic 并且没有人给出真正的解决方案。

当用户点击按钮时,按钮被禁用:

  <div style="overflow: hidden;">
      <p class="mb-1 pull-left mt-5">Select up to ten audiences</p>
       <button class="btn-text mt-5 mb-1 pull-right" id="reloadSuggestions"><i class="far fa-sync-alt"></i></button>
  </div>



$("#reloadSuggestions").click(function(e) {

    fetchSuggestedTargeting();
    $("#reloadSuggestions").prop("disabled", true);
});

如果用户没有填写输入,我再次启用按钮:

function fetchSuggestedTargeting() {

    // set array of keywords
    var tagsinputKeywords = $("#interestTagInput").tagsinput('items')

    // Make sure user entered keywords
    if (tagsinputKeywords.length < 1) {

        $("#reloadSuggestions").prop('disabled', false);
        $("#searchSuggestedInterest").prop('disabled', false);;

        swal({
            title: "oops!",
            text: "Please enter atleast 1 keyword that describes your product.",
            imageUrl: "/alert-icon.png"
        });

        return;
    }
}

here is an example of the issue

编辑:

为了澄清 SO 的电源跳闸模块,我试图在单击按钮后重新启用按钮并用于异步获取数据,一旦获取数据,我想为用户重新启用按钮以便能够再次单击它。

当尝试通过运行$("#reloadSuggestions").prop('disabled', false);disabled 属性设置回false 时,没有发生

不确定这与有人询问如何更改属性的重复,这里的问题是属性不会更改,无论正在运行什么代码,除非我将其包装在超时中。

【问题讨论】:

标签: javascript jquery


【解决方案1】:

发现如果我将代码包装在超时中:

setTimeout(function () {

   $("#reloadSuggestions").prop('disabled', false);
}, 0);

我的代码可以工作,我想到了这个解决方案,因为我正在阅读 this question 来解决类似的问题

当您设置超时时,它实际上将异步代码排队,直到引擎执行当前调用堆栈。

希望这对遇到此烦人问题的其他人有所帮助。

由于显然不存在错误,here is an example of the issue

【讨论】:

  • @PatrickEvans 代码正在单击侦听器上运行,元素已加载并在单击以获取数据时被禁用,然后在获取数据时假定启用按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-02
  • 2020-04-15
  • 2011-08-28
  • 2018-10-12
相关资源
最近更新 更多