我正在使用一个 jQuery 来设置我的复选框的样式,这意味着,在单击时,它会更改复选框的样式(实际上是 onchange)。我的问题是:我选择的值一方面来自数据库,另一方面由用户修改。因此,如果满足条件,我希望选中该单选按钮。我想知道如何在我的 jQuery 函数中做到这一点:(下面的代码:)
<script type="text/javascript">
$(document).ready(function(){
$(".CheckBoxClass").change(function(){
if($(this).is(":checked")){
$(this).next("label").addClass("LabelSelected");
}else{
$(this).next("label").removeClass("LabelSelected");
}
});
});
</script>
和 HTML:
<form id="address" method="POST" action="<?= Route::url('Save user preferences' , array('user_id' => $user));?>">
<? foreach ($prefered_products as $pp): ?>
<input id="CheckBox[<?= $pp; ?>]" type="checkbox" class="CheckBoxClass" style="display: none;" name="user_preferences_preference[<?= $pp->id ?>]" value="<?= $pp ?>" checked="checked" />
<label id="Label[<?= $pp; ?>]" for="CheckBox[<?= $pp; ?>]" class="CheckBoxLabelClass"><?= $pp->product; ?></label>
萨尔维萨喜欢的故事
该功能只是onchange。我还希望,如果存在 attr cheched = "checked",则要选中的复选框(要显示的选中样式)。
在给定的条件下这可能吗?