【问题标题】:Disable Button with a bind function?使用绑定功能禁用按钮?
【发布时间】:2022-10-12 22:22:35
【问题描述】:

您好,我怎样才能禁用具有绑定功能的按钮 10 秒?

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

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      这是一个简单的 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);
      }
      &lt;button id="GO"&gt;GO!&lt;/button&gt;

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

        • 嘿 Barmar 非常感谢你。停用有效,但不能使用此代码重新激活。你有什么想法吗?
        • 我已经将答案从属性更改为属性,我认为这会起作用。
        猜你喜欢
        • 1970-01-01
        • 2015-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-04
        • 1970-01-01
        相关资源
        最近更新 更多