我有一个带有复选框的选择 div,我正在尝试使用引导按钮和 jQuery 来设置它们的样式。每个 input checkbox
都采用 php 的属性 "checked"
。所以我需要的是:
- 找到所有“选中”的
输入
,转到它们的parent
标签,最后addClass active
。
我的 html(在 php foreach 循环中)
<?php
global $wpdb;
$ss = "select * from ".$wpdb->prefix."project_email_alerts where uid='$uid'";
$rr = $wpdb->get_results($ss);
$terms = get_terms( 'project_cat', 'parent=0&orderby=name&hide_empty=0' );
foreach($terms as $term):
$chk = (KleeiaDev_check_list_emails($term->term_id, $rr) == true ? "checked='checked'" : "");?>
<div data-toggle="buttons" class="btn-group bizmoduleselect">
<label id="b-<?php echo $term->term_id; ?>" class="btn btn-default">
<div class="bizcontent">
<input id="a-<?php echo $term->term_id ?>" type="checkbox" name="email_cats[]" <?php echo $chk ?> value="<?php echo $term->term_id ?>" />
<span class="glyphicon glyphicon-ok glyphicon-lg"></span>
<h5><?php echo $term->name ?></h5>
</div>
</label>
</div>
<?php endforeach; ?>
我以这种方式尝试使用 jQuery,但没有成功搜索其他类似问题:
if($('input').is(':checked')){
jQuery(this).parent('label').addClass('active');
}
我尝试过使用 .each 但不太确定。
最佳答案
你可以使用
$('input[type=checkbox]:checked').parents('label').addClass('active');
$('input[type=checkbox]:checked').parents('label').addClass('active');
.active{
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<label >
<input type='checkbox' checked='true'> Check me
<label>
</div>
<div>
<label>
<input type='checkbox'> Check me
<label>
</div>
关于javascript - jquery 查找特定输入的父级并应用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38672029/