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();
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作