[英]Hide the text field,when selecting a value in dropdown
I want to make a text field shown when selecting the value "shift" in the drop down.If the value is "workoff",the text field become hidden.Here is the code that I did.It is not working.How to solve it ? 我想在下拉列表中选择值“ shift”时显示一个文本字段。如果值是“ workoff”,则该文本字段变为隐藏状态。这是我所做的代码。它不起作用。如何解决呢?
View: 视图:
<div class="form-group col-md-4">
<label class="empshift col-md-12 control-label">Shift Type</label>
<div class="col-sm-6">
<?php
$designationsOptionsJs = 'id="shiftTypeId" class="form-control shiftType"';
echo form_dropdown('shiftTypeId', $shifttypes, '',$designationsOptionsJs);
?>
</div>
</div><!--/form-group-->
<div class="form-group col-md-4" id="shift_id" style="display:none;">
<label class="empshift col-md-12 control-label">Shifts</label>
<div class="col-sm-6">
<?php
$designationsOptionsJs = 'id="shiftId" class="form-control shiftdepartment"';
echo form_dropdown('shiftId', $shifts, '',$designationsOptionsJs);
?>
</div>
</div><!--/form-group-->
script: 脚本:
<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>
you must use like this for dynamic created elements, also you are displaying the element only when value is 'Shift' so make your code simple like 您必须对动态创建的元素使用这样的控件,并且仅在value为'Shift'时才显示该元素,因此使您的代码简单如
$(document).on("change","#shiftTypeId",function(){
$('#shift_id').hide();
if($(this).val() == "Shift"){
$('#shift_id').show();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.