选择此内的所有输入,标签,选择等 - 每个循环

Ila*_*sda 8 html javascript jquery jquery-selectors

我目前正在研究一些复杂的形式.

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

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

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

});
Run Code Online (Sandbox Code Playgroud)

我想选择所有输入this(当前循环.selector).我这样做了吗?

Bol*_*ock 17

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

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

如果您唯一要做的就是在这些元素上设置该属性,那么您实际上并不需要.each()循环.你可以放心地将它减少到这个单线:

$('input, select, label, textarea', '.selector').prop('disabled', true);
Run Code Online (Sandbox Code Playgroud)