简体   繁体   English

Javascript针对多个元素

[英]Javascript target multiple elements

I have the following JS: 我有以下JS:

  function init_selectAllFull(){
        $('#select-all-perms input, #user_full_access_to_all_shows').click(function() {
            var checked = $(this).is(':checked');
            $('#show-permissions-list').find('input[type=checkbox].full_access').each(function() {
            if (checked) {
                $(this).prop("checked", true);
            } else {
                $(this).removeAttr("checked");
                $(".internal.external[value='none']").prop('checked', true);
                $(".external[value='none']").prop('checked', true);
                $(".internal[value='none']").prop('checked', true);
                $(".external.internal[value='none']").prop('checked', true);

            }
                $(this).trigger("change");
            });
        });
  }

It checks a bunch of radio fields when user unchecks a field. 当用户取消选中某个字段时,它将检查一堆无线电字段。 This works but I don't like the following code, there's too much repetition going on. 这可行,但是我不喜欢下面的代码,重复太多了。

$(".internal.external[value='none']").prop('checked', true);
$(".external[value='none']").prop('checked', true);
$(".internal[value='none']").prop('checked', true);
$(".external.internal[value='none']").prop('checked', true);

Is there a cleaner way to target multiple classes? 有没有针对多个类别的更清洁方法? any help would be greatly appreciated. 任何帮助将不胜感激。 thank you so much! 非常感谢!

You can chain selectors in jquery like so: 您可以像这样在jquery中链接选择器:

$(".internal.external[value='none'], .external[value='none'], .internal[value='none'], .external.internal[value='none']").prop("checked", true);

Think of it like in a CSS file, where you can style multiple classes or elements by separating the selectors of a style block with commas: 就像在CSS文件中一样,您可以在其中通过用逗号分隔样式块的选择器来设置多个类或元素的样式:

.row1, .row2 {
    border: 1px solid blue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM