2014-05-22 20 views
0

这是我复选框我怎样才能让我的复选框显示隐藏的范围,如果它被选中

<?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;' 
     ); 
?> 

当我的复选框被选中它显示了一个隐藏的范围,并取消选中时,它隐藏的跨度。我已经完成了这个使用JavaScript。

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

    if (this.checked) { 

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

    } else { 

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

此功能适用于如果我检查或取消选中该框。但是当页面加载时,我需要一个函数来检查复选框是否被选中,从而隐藏或显示已经被赋予了一类#hrsObs的范围。有什么建议么?

+1

只需添加'.change()'你的最后一个代码块的结束,你就完成了。 '('#Heart_Rate')。on(“change”,function(){...})。change(); // <---'这会立即触发处理程序。 –

+0

想知道答案是否有帮助... –

回答

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

请详细说明OP如何使用此代码来解决问题。 –

0

可以在DOM ready事件使用:

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

试试这个:

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(); 

    }); 
相关问题