Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery,获取';这';来自条件的选择器_Javascript_Jquery_Checkbox_Input_Jquery Selectors - Fatal编程技术网

Javascript Jquery,获取';这';来自条件的选择器

Javascript Jquery,获取';这';来自条件的选择器,javascript,jquery,checkbox,input,jquery-selectors,Javascript,Jquery,Checkbox,Input,Jquery Selectors,我的页面上有几个“checkbox”类元素。单击时,会选中相应的复选框输入。但是,我需要让JQuery在页面第一次加载时检查checkbox元素是否处于活动状态,并在那时相应地检查checkbox输入 由于我的页面上有多个“checkbox”类,我以前使用过“this”选择器,它工作得很好,但是我不知道如何在没有.click操作的情况下使用我的条件页面加载来实现这一点。以下是我想让它发挥作用的地方: if($('.checkbox').hasClass('active')) { $('[

我的页面上有几个“checkbox”类元素。单击时,会选中相应的复选框输入。但是,我需要让JQuery在页面第一次加载时检查checkbox元素是否处于活动状态,并在那时相应地检查checkbox输入

由于我的页面上有多个“checkbox”类,我以前使用过“this”选择器,它工作得很好,但是我不知道如何在没有.click操作的情况下使用我的条件页面加载来实现这一点。以下是我想让它发挥作用的地方:

if($('.checkbox').hasClass('active')) {
    $('[name="'+$(this).attr('rel')+'"]').prop('checked', true); 
}

显然,“this”选择器不知道我指的是什么。有更好的方法吗?因为它检查了一堆元素,而不仅仅是一个元素,我被难住了。谢谢

您只能在jQuery函数的上下文中使用
这个
,因此在此范围内,它不会引用任何
.checkbox

您可以使用。每个:

$('.checkbox.active').each(function() {
    // In this context, this refers to the DOM element represented by .checkbox.active
    $('[name="'+this.rel+'"]').prop('checked', true); 
});

您只能在jQuery函数的上下文中使用
this
,因此在此范围内,它不会引用任何
.checkbox

您可以使用。每个:

$('.checkbox.active').each(function() {
    // In this context, this refers to the DOM element represented by .checkbox.active
    $('[name="'+this.rel+'"]').prop('checked', true); 
});

每个
功能可能适合您的需要:

$('.checkbox').each(function() {
    var $this = $(this);
    if ($this.hasClass('active')) {
        $('[name="'+$this.attr('rel')+'"]').prop('checked', true); 
    }
});
如果在
活动
类不存在时必须取消选中复选框,则:

$('.checkbox').each(function() {
    var $this = $(this);
    $('[name="'+$this.attr('rel')+'"]').prop('checked', $this.hasClass('active')); 
});

每个
功能可能适合您的需要:

$('.checkbox').each(function() {
    var $this = $(this);
    if ($this.hasClass('active')) {
        $('[name="'+$this.attr('rel')+'"]').prop('checked', true); 
    }
});
如果在
活动
类不存在时必须取消选中复选框,则:

$('.checkbox').each(function() {
    var $this = $(this);
    $('[name="'+$this.attr('rel')+'"]').prop('checked', $this.hasClass('active')); 
});

FWIW,
rel
有一个反射属性,因此
$(this.attr('rel')
最终成为一种迂回的书写方式
this.rel
:-)你们是巫师。这很有效。非常感谢!(我会在4分钟内接受,如果它允许的话)@T.J.Crowder Nice!我没有意识到,如果它有一个反射属性,那么您可能应该使用
.prop
而不是
.attr
@michaelb958 TJ,因为DOM元素已经有一个公开的
rel
property.FWIW,
rel
有一个反射属性,所以
$(this.attr('rel'))
最终成为一种迂回的书写方式
this.rel
:-)你们是巫师。这很有效。非常感谢!(我会在4分钟内接受,如果它允许的话)@T.J.Crowder Nice!我没有意识到,如果它有一个反射属性,那么您可能应该使用
.prop
而不是
.attr
@michaelb958 TJ,因为DOM元素已经有一个公开的
rel
属性,所以不需要prop或attr。