duanqu9292 2013-05-15 18:44
浏览 88
已采纳

复选框在刷新/提交后保持检查状态

if so to trigger a js function. I can make a checkbox stay checked with value and checked="checked" on page refresh and for my submit the following...

 <input type="checkbox" name="check" value="checked" 
 <?php if($_POST['check'] ==    'checked'): ?>checked="checked"<?php endif; ?> />

thou neither of this remember to trigger the js function.

Here is my function and what I try to accomplish. http://jsfiddle.net/melbourne/s5AXZ/

My only choices are what Im asking you or to show and post new comments via ajax.. still the refresh problem will remain but the refresh will not be needed so much. In a nutshell Im sticking with my first question since ajax posting and showing new comments its out of my league. Thanks.

  • 写回答

1条回答 默认 最新

  • duangang3832 2013-05-15 18:56
    关注

    Your question is kind of messy, but from what I understand do you wish to fire the .change-method if the checkbox is checked at load.

    I'd suggest you just do the same php-if on the parent-element (the one you are adding the night-class to).

    Something like:

    <?php if($_POST['check'] == 'checked') echo 'class="night"'; ?>
    

    It is much better to fix this at serverside when you already know the changes you wish to do.

    BUT, if you want to do this with only js, you can do it like this:

    function initBackground() {
        if ($('#btn').is(':checked')) {
            $('#btn').parent().toggleClass('night');
        }
    }
    
    $(document).ready(function () {
        initBackground();
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?