[英]How can i make my checkbox show a hidden span if it is checked
这是我的复选框
<?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类的范围。 有什么建议么?
$('#Heart_Rate').attr('checked') // give you you the information
您可以在DOM ready事件中使用此命令:
$(document).ready(function(){
if($('#Heart_Rate:checked').length){
$('#hrsObs').fadeIn('fast');
}});
试试看:
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.