在 if 语句中使用此父项 jquery

Using this parent in an if statement jquery

我试图触发对父级 div 的点击,仅在选中了复选框的父级上。我不能使用 'this',因为这会使“.choose-plain”触发点击它。有没有办法将它用于 if 语句中的元素而不是点击?

$('.choose-plain').click(function(){
    if ($('.option-other').is(':checked')) {
        $(the parent div of the if statement only if .option-other is checked).trigger( "click" );
    } 
});

html/php

    <div id="special_feature_select" class="option_container choose choose-plain">
        <h5>Plain</h5>
        <img src="/img/decor/swatch/white.png"/>
        <?= $form->input('',array('name' => 'data[Pattern][plain]','value' => 1,'class' => 'option_value option-plain', 'type' => 'checkbox',
     'label' => false,'checked' => $pattern==1?'checked':false,'div' => false));?>
    </div>

    <div id="special_feature_select" class="option_container choose choose-other">
    <h5>Patterned</h5>
        <img src="/img/decor/swatch/patterned.png"/>
        <?= $form->input('',array('name' => 'data[Pattern][patterned]','value' => 254,'class' => 'option_value option-other', 'type' => 'checkbox',
     'label' => false,'checked' => $pattern==254?'checked':false,'div' => false));?>
    </div>

    <div id="special_feature_select" class="option_container choose choose-other">
        <h5>Childrens</h5>
        <img src="/img/decor/swatch/childrens.png"/>
        <?= $form->input('',array('name' => 'data[Pattern][childrens]','value' => 64,'class' => 'option_value option-other', 'type' => 'checkbox',
     'label' => false,'checked' => $pattern==64?'checked':false,'div' => false));?>
    </div>

因为复选框是隐藏的,所以我必须触发对父 div 的点击,这在我的 jquery 的另一部分触发了复选框的选中或取消选中。

根据您的标记,您的标记无效,因为您在多个 div 上应用了相同的 ID。 每个元素的 ID 应该是唯一的

你可以使用这个:

$('.choose-plain').click(function(){
     var chcked = $(this).find(':checkbox').prop('checked');
     $('.option-other').prop('checked', !chcked);
});