我有一个选择div,里面有复选框,我正试图用引导按钮和jQuery来设计它们的样式。每个input checkbox
通过php获取属性"checked"
。所以我需要的是:
- 找到所有"已检查"的
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');
}
我试过使用。每个都有,但不是很确定。
您可以使用
$('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>
也许在客户端添加类很重要,但在服务器端也可以这样做:
$chk = (KleeiaDev_check_list_emails($term->term_id, $rr) == true ? "checked='checked'" : "");?>
<label id="b-<?php echo $term->term_id; ?>" class="btn btn-default <?=!empty($chk) 'active' : ''?>">