代码之家  ›  专栏  ›  技术社区  ›  Imnotapotato

如何在禁用的button元素上触发click事件(使用纯vanilla JS)?

  •  0
  • Imnotapotato  · 技术社区  · 7 年前

    this.show.onclick = this.sendData.bind(this);

    在我的 bindEvents() 功能:

    bindEvents: function() {
    
        var that = this;
    
        // On click "Show" BTN
        this.show.onclick = this.sendData.bind(this);
    
        // On Change inputs
        this.$form.change(function(){
            that.updateDatesInputs(this);
        });
    },
    

    sendData: function(e) {
        e.preventDefault();
        let that                = this;
    
        console.log(this.show.disabled);
        if (this.show.disabled) {
            alert('a disabled button has just been clicked!');
            this.showErrorDiv("Please select a new date range.");
        } else {
    
            $.ajax({
                        ....
                }
            });
    
            that.dataDisplayed = true;
    }
    

    单击我的“show”按钮元素不会激活任何单击事件。我在google上找到的只是jQuery可以修复它,但我想使用vanilla JS。

    如何在禁用的元素上触发事件,以便仅使用纯JS执行警报?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Chris Ferdinandi    6 年前

    不能这样做,也不应该因为无障碍的原因。一个用键盘导航的人永远无法用这个按钮与它交互。

    推荐文章