【问题标题】:Select all inputs, labels, selects etc within THIS - each loop选择 THIS 中的所有输入、标签、选择等 - 每个循环
【发布时间】:2011-12-28 18:19:29
【问题描述】:

我现在正在处理一些复杂的表格。

只是想知道,有没有更好的方法来做到这一点:

$('.selector').each( function(){

    $("input", this).prop('disabled', true);
    $("select", this).prop('disabled', true);
    $("label", this).prop('disabled', true);
    $("textarea", this).prop('disabled', true);

});

我想选择this 中的所有输入(当前循环通过.selector)。 我这样做正确吗?

【问题讨论】:

  • 是的,从技术上讲。尽管@BoltClock 下面的回答是一种更好的方法。

标签: javascript jquery html jquery-selectors


【解决方案1】:

这很好,但为了简化它,您应该能够像对任何其他选择器进行分组一样使用逗号:

$('.selector').each(function() {
    $('input, select, label, textarea', this).prop('disabled', true);
});

如果您唯一要做的就是在这些元素上设置该属性,那么您实际上并不需要 .each() 循环。您可以放心地放弃它并将其简化为单行:

$('input, select, label, textarea', '.selector').prop('disabled', true);

【讨论】:

  • $(".selector input, .selector select, .selector label, .selector textarea").prop('disabled', true); 是否等效?
  • @Brian M. Hunt:应该是。另外,$('.selector').find('input, select, label, textarea').prop('disabled', true);
  • 整洁。我想知道它们之间是否有任何性能差异。
猜你喜欢
  • 2016-04-20
  • 1970-01-01
  • 2012-04-14
  • 2017-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 1970-01-01
相关资源
最近更新 更多