我的目标:
我有一个模式,我想用它来向数据库添加工作职位。我选择开始日期
,并选中still-here
复选框应该隐藏 ending date
字段并在其位置显示一个 div,上面写着 I currently work here
。
我的问题:
是不是我的 jQuery 没有在模态上获取 dom 元素...我在复选框上有 console.log()
以查看是否被选中,但它没有工作得那么好。它只提供模式启动时的初始值,而不是每次我选择/取消选择复选框时的值。
查看我的代码示例 here
我的问题:
我错过了什么,为什么它不起作用,我怎样才能实现我的目标?
试试这个
$(document).ready(function() {
$('#myModal').on('shown.bs.modal', function () {
$(".current-position").hide();
console.log($('#still_here').is(':checked'));
});
});
$('#still_here').on('change',function(){
if($('#myModal .modal-body #still_here').prop('checked')){
$("#myModal .modal-body .current-position").show();
console.log($("#myModal .modal-body #to_month"));
$("#myModal .modal-body #to_month").hide();
$("#myModal .modal-body #to_year").hide();
}else{
$("#myModal .modal-body .current-position").hide();
$("#myModal .modal-body #to_month").show();
$("#myModal .modal-body #to_year").show();
}
});
我是一名优秀的程序员,十分优秀!