在 if 语句 jquery 中使用此父级



我试图仅在选中了内部复选框的父级上触发对父级div 的单击。我不能使用"这个",因为这会使".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" );
    } 
});

网页/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);
});

最新更新