最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select all inputs, labels, selects etc within THIS - each loop - Stack Overflow

programmeradmin3浏览0评论

I'm working on some plicated form at the moment.

Just wondering, is there any better way to do this:

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

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

});

I want to select all inputs within this (currently looped through .selector). Am I doing this correctly?

I'm working on some plicated form at the moment.

Just wondering, is there any better way to do this:

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

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

});

I want to select all inputs within this (currently looped through .selector). Am I doing this correctly?

Share Improve this question edited Jan 23, 2013 at 18:19 BoltClock 724k165 gold badges1.4k silver badges1.4k bronze badges asked Nov 14, 2011 at 15:32 IladarsdaIladarsda 10.7k40 gold badges108 silver badges171 bronze badges 1
  • 1 Yes, technically. Although @BoltClock's answer below is a better way of doing this. – rossipedia Commented Nov 14, 2011 at 15:34
Add a ment  | 

1 Answer 1

Reset to default 17

That's fine, but to simplify it you should be able to use the ma as you would to group any other selectors:

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

If the only thing you're doing is setting that property on those elements, then you don't really need the .each() loop. You can safely drop that and reduce it to this one-liner:

$('input, select, label, textarea', '.selector').prop('disabled', true);
发布评论

评论列表(0)

  1. 暂无评论