Javascript 在下拉列表中选择值时隐藏文本字段

Javascript 在下拉列表中选择值时隐藏文本字段,javascript,codeigniter,drop-down-menu,Javascript,Codeigniter,Drop Down Menu,我想在下拉列表中选择值“shift”时显示一个文本字段。如果值为“workoff”,文本字段将隐藏。这是我所做的代码。它不起作用。如何解决 视图: 轮班制 转移 脚本: <script type="text/javascript"> $(document).ready(function(){ $("#shiftTypeId").change(function(){ var shiftTypeId = $(this).val();

我想在下拉列表中选择值“shift”时显示一个文本字段。如果值为“workoff”,文本字段将隐藏。这是我所做的代码。它不起作用。如何解决

视图:


轮班制
转移
脚本:

<script type="text/javascript">
$(document).ready(function(){

        $("#shiftTypeId").change(function(){
            var shiftTypeId = $(this).val();
            if(shiftTypeId == "Shift"){
                $('#shift_id').show();

            }else if(shiftTypeId == "Workoff"){
                $('#shift_id').hide();

            }else{
                $('#shift_id').hide();

            }
        });
        });

        </script>

$(文档).ready(函数(){
$(“#shiftTypeId”).change(函数(){
var shiftTypeId=$(this.val();
if(shiftTypeId==“Shift”){
$('shift_id').show();
}else if(shiftTypeId==“Workoff”){
$('shift_id').hide();
}否则{
$('shift_id').hide();
}
});
});

对于动态创建的元素,您必须这样使用,而且您仅在值为“Shift”时才显示元素,因此代码要像这样简单

$(document).on("change","#shiftTypeId",function(){
    $('#shift_id').hide();
    if($(this).val() == "Shift"){
        $('#shift_id').show();
    }
});

对于动态创建的元素,必须这样使用,而且只有当值为“Shift”时才显示元素,所以代码要像

$(document).on("change","#shiftTypeId",function(){
    $('#shift_id').hide();
    if($(this).val() == "Shift"){
        $('#shift_id').show();
    }
});

@Saclt7检查$shifttypes中的选项值,可能是值不同,也可能是实际情况sensetive@Saclt7检查$shifttypes中的选项值,可能是值不同或大小写敏感