【问题标题】:Referencing multiple elements in function using 'this' - JavaScript使用“this”引用函数中的多个元素 - JavaScript
【发布时间】:2014-09-19 02:22:41
【问题描述】:

取这个函数:

$(document).on('change', '#ele1, #ele2', function() {
    if (this.value == "") {
        $('.link').bind('click', false);
    } else {
        $('.link').unbind('click', false);
    }
});

如何使用this 分别引用元素#ele1#ele2

【问题讨论】:

  • 您可以传递事件并使用e.target
  • @elclanrs 你能举个例子吗
  • 你最终想要达到的目标是什么?
  • @LeeTaylor 我想要一种简单的方法来检查 ele1ele2 的值是否为空 - 用于验证。
  • this 指的是当前更改的元素 - jsfiddle.net/arunpjohny/08dqo0um/1

标签: javascript jquery this event-delegation


【解决方案1】:

正如 ArunPJohny 指出的 this 仅引用当前更改的元素,所以我只是直接引用它们。

$(document).on('change', '#group_name, #dept-name-input', function() {
    if ( $('#group_name').val() == "" || $('#dept-name-input').val() == "") {
        $('.add-td').bind('click', false);
    } else {
        $('.add-td').unbind('click', false);
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2011-12-28
    • 2011-08-07
    • 2021-03-21
    • 1970-01-01
    • 2019-02-13
    • 2017-03-01
    相关资源
    最近更新 更多