我有一个带有复选框的选择div,我试图用引导按钮和jQuery设置样式。每个input checkbox
都"checked"
通过php获取属性。所以我需要的是:
inputs
“已检查”的内容,转到其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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句