【问题标题】:jQuery attach event only when anchor is not disabled仅当未禁用锚点时 jQuery 附加事件
【发布时间】:2014-02-27 06:47:52
【问题描述】:

我尝试附加和事件,然后检查锚标记是否被禁用然后不做任何事情,否则 doSomething()
这是我到目前为止所做的事情,结果符合预期;

<a href="#" disabled="true" class="btn btn-small btn-danger remove"><i class="icon-remove"></i></a>

这是我的 jQuery;

acitivityListDataTable.find('tbody').on('click', 'tr td a.remove', function (e) {
                e.preventDefault();
                var $this = $(this);
                if ($this.attr('disabled')) {
                    return false;
                } else {
                    var link = $this.attr('href');
                    bootbox.confirm("Are you sure you want to delete this?", function (result) {
                        if (result) {
                            window.location.href = link;
                            //milestoneTable.fnDraw();
                        }
                    });
                }

            });


但是,我认为如果锚标签被禁用,我什至不应该附加该事件是好的(或者纠正我,如果我错了);
所以,这是我尝试过的,但它不工作,点击事件仍在工作,即附加。

acitivityListDataTable.find('tbody').on('click', 'tr td a.remove:not(:disabled)', function (e) {
                e.preventDefault();
                var $this = $(this);
                    var link = $this.attr('href');
                    bootbox.confirm("Are you sure you want to delete this?", function (result) {
                        if (result) {
                            window.location.href = link;
                        }
                    });

            });

区别只在这里tr td a.remove:not(:disabled)
更新: 我把锚改成了这个;

<a href="#" disabled="disabled" class="btn btn-small btn-danger remove"><i class="icon-remove"></i></a>

【问题讨论】:

  • 您不能使用disable="true"禁用链接
  • 它的工作和禁用,锚在 ui 中显示禁用
  • disabled="true" 不是&lt;a&gt; 标签的有效属性
  • @DotNetDreamer 你确定吗? jsfiddle.net/4ksfr
  • 是的,我试过disabled="disabled",但还是没有运气

标签: jquery performance events


【解决方案1】:

如果我理解正确,您希望防止将点击事件绑定到任何具有“已禁用”属性(或不选择这些元素)的 &lt;a&gt; 标记

我在这里实现了这一点:http://jsfiddle.net/LL8yd/

相关代码为:

$('div').on('click', ':not(a[disabled])', function(){ /*..*/ });

【讨论】:

    【解决方案2】:

    @Jack 是对的,我像下面这样调整代码并且它正在工作:)

    acitivityListDataTable.find('tbody').on('click', 'tr td a.remove:not([disabled])', function (e) {
                e.preventDefault();
                var $this = $(this);
                    var link = $this.attr('href');
                    bootbox.confirm("Are you sure you want to delete this?", function (result) {
                        if (result) {
                            window.location.href = link;
                            //milestoneTable.fnDraw();
                        }
                    });
    
            });
    

    【讨论】:

      猜你喜欢
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      相关资源
      最近更新 更多