开发者

Ui checkbox - checked by default

开发者 https://www.devze.com 2023-03-15 16:32 出处:网络
I am using a jQuery that is styling my checkbox, meaning that, on click, it changes the style of the checkbox (onchange actually).

I am using a jQuery that is styling my checkbox, meaning that, on click, it changes the style of the checkbox (onchange actually). My problem is: the values I select are taken from a database, on one part, and modified by users on the other part. So, if a condition is fulfilled, I want that radio button to be checked. I wonder how can I do this in my jQuery function: (code below:)

  <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>

And t开发者_Python百科he 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>

Salveaza preferintele tale

The function is only onchange. I also want that, if exists the attr cheched = "checked", the checkbox to be checked (the checked style to be displayed).

Is this possible in the given conditions?


$(".CheckBoxClass:checked").each(function(){
  $(this).next('label').addClass("LabelSelected");
});


You could manually trigger the change effect after your .change() is declared, untested but should work:

$(".CheckBoxClass").trigger('change');
0

精彩评论

暂无评论...
验证码 换一张
取 消