简体   繁体   English

如果选中复选框,如何使复选框显示隐藏的跨度

[英]How can i make my checkbox show a hidden span if it is checked

this is my checkbox 这是我的复选框

<?php  $create = array(
           'name'=> 'single_obs_value',
           'id' => 'Heart_Rate',
           'class' => 'singleobs',
           'value' => $cp->odvalue_list_id,
           'checked' => sel_checkbox($singleobsnormal['single_obs_value'] == $cp->odvalue_list_id),
           'style'=> 'float: left; margin-right: 10px;'
       );
?>

When my checkbox is ticked it shows a hidden span and when unticked it hides the span. 勾选我的复选框时,它会显示一个隐藏的跨度;而当未选中时,它会隐藏该跨度。 I have done this using javascript. 我已经使用javascript完成了此操作。

$('#Heart_Rate').on("change", function() {  

    if (this.checked) {

         $('#hrsObs').fadeIn('fast');

    } else {

        $('#hrsObs').fadeOut('fast');
    }
}   

This function works perfectly for if i am checking or unchecking the box. 如果我选中或取消选中此框,此功能将非常适合。 But when the page loads i need a function to check if the checkbox was checked or not and thus hide or show the span which has been given a class of #hrsObs. 但是,当页面加载时,我需要一个函数来检查复选框是否被选中,从而隐藏或显示已被赋予#hrsObs类的范围。 Any suggestions? 有什么建议么?

$('#Heart_Rate').attr('checked') // give you you the information

You can use this in DOM ready event: 您可以在DOM ready事件中使用此命令:

$(document).ready(function(){
 if($('#Heart_Rate:checked').length){
   $('#hrsObs').fadeIn('fast');
}});

Give this a try: 试试看:

jQuery(document).ready(function(){ 

        //cache these since you'll be using them a few times
        var $heartRate  = $('#Heart_Rate'),
            $hrsObs     = $('#hrsObs');

            $heartRate.on("change", isChecked());

            function isChecked(){
                if($heartRate.prop("checked")){
                    $hrsObs.fadeIn('fast');
                }
                else{
                    $hrsObs.fadeOut('fast');
                }
            }

            //run function now that page has loaded
            isChecked();

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM