简体   繁体   English

根据所选选项显示输入字段

[英]Display input field based on selected option

I'm trying to display and hide certain value based on specific option selected. 我试图根据所选的特定选项显示和隐藏某些值。 The options are retrieved from the database. 这些选项是从数据库中检索的。 But I can't get it to work with this code below. 但是我无法在下面的代码中使用它。 Can you help me find out where I'm going wrong? 您能帮我找出问题所在吗? I'd like to try and get this code working. 我想尝试使此代码正常工作。

Here is the picture of what I have been working on. 这是我一直在努力的图片。 What I would like to do is, when I select the leave type (Eg: Annual leave), the red A box will show and hide the blue B box & when I select Time Off leave type, the blue B box will show and red A box will hide 我想做的是,当我选择休假类型(例如:年假)时,红色的A框将显示并隐藏蓝色的B框;当我选择“休假”休假类型时,蓝色的B框将显示而红色一个盒子会藏起来

Here is my current codes: 这是我当前的代码:

 <script> function updateValue(value) { document.getElementById('ID').value = value } // set a defalt value updateValue(document.getElementById('leavetype').value) </script> 
 <?php $sql="SELECT * FROM leave_balance WHERE passcodeEmp='$passcodeEmp'"; $result1= mysqli_query($connect, $sql); ?> <div class="form-group"> <label class="col-sm-2 control-label">Leave Type</label> <div class="col-sm-8"> <select id="leavetype" class="form-control" name="balanceID" onchange="updateValue(this.value)" required/> <option selected disabled>Select Leave Type</option> <?php while($row1 = mysqli_fetch_array($result1)){ $balanceID = $row1['balanceID']; $leaveName = $row1['leaveName'];?> <option value="<?php echo $balanceID?>"><?php echo $leaveName;?></option><br> <?php }?> </select> </div> </div> 

This code used to show the red A box 此代码用于显示红色的A框

 <div class="form-group"> <label class="col-sm-2 control-label">From Date</label> <div class="col-sm-3"> <div class="input-group"> <div class="input-group-addon"><i class="fa fa-calendar"></i> </div><input type="date" class="form-control" id="datepicker" name="fromDate" required/ > </div> </div> <label class="col-sm-2 control-label">To </label> <div class="col-sm-3"> <div class="input-group"> <div class="input-group-addon"><i class="fa fa-calendar"></i> </div><input type="date" class="form-control" id="datepicker" name="endDate" required/ > </div> </div> </div> 

Try This 尝试这个

function updateValue(value)
{
    var selectedOption = document.querySelector('option[value="'+value+'"]').text;
    if(selectedOption == 'Time Off'){
        // show (blue B box) and hide (red A box)
    }else{
        // show (red A box) and hide (blue B box)
    }
}

// set a defalt value
updateValue(document.getElementById('leavetype').value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM