我试图根据所选的特定选项显示和隐藏某些值。从数据库中检索选项。但我无法使用下面的代码。你能帮我找出我出错的地方吗?我想尝试让这段代码正常运行。
这是我目前的代码:
<script>
function updateValue(value)
{
document.getElementById('ID').value = value
}
// set a defalt value
updateValue(document.getElementById('leavetype').value)
</script>
&#13;
<?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>
&#13;
此代码用于显示红色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>
&#13;
答案 0 :(得分:0)
试试这个
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)