如何禁用选择“pelajaran”,其中条件选择jabatan =“operator”
<select name="id_jabatan" id="jabatan" size="1" class="form-control" required>
<option label="-- Pilih Jabatan --"></option>
<?php //query untuk looping jabatan
$sql = "SELECT * FROM tb_jabatan_guru";
$result = $koneksi->query($sql);
while ($row = $result->fetch_array()){ ?>
<option value="<?php echo $row['idJabatan'] ?>"><?php echo $row['namaJabatan']; ?></option>
<?php } ?>
</select>
<select name="id_pel" id="pelajaran" size="1" class="form-control" disabled required>
<option label="-- Pilih Pelajaran --"></option>
<?php //query untuk looping pelajaran
$sql = "SELECT * FROM tb_pelajaran";
$result = $koneksi->query($sql);
while ($row = $result->fetch_array()){ ?>
<option value="<?php echo $row['idPelajaran'] ?>"><?php echo $row['namaPelajaran']; ?></option>
<?php } ?>
</select>
<script>
document.getElementById('jabatan').onchange = function () {
document.getElementById("pelajaran").disabled = this.value == 'Operator';
}
</script>
答案 0 :(得分:0)
请检查,
document.getElementById('jabatan').onchange = function () {
if(this.value == 'Operator'){
document.getElementById('pelajaran').disabled = true;
}
}
答案 1 :(得分:0)
你好Fahruddin Yusuf Habibi
我要告诉你它对我有用的方式。我希望它也适合你。我的代码是这样的:
<select onChange="showSelect();" name="id_jabatan" id="jabatan" size="1" class="form-control" required>
</select>
<div id="selectDisable" style="display:none">
<select name="id_pel" id="pelajaran" size="1" class="form-control" disabled required>
</select>
</div>
<script>
function showSelect(){
var value = document.getElementbyId('jabatan');
if(value == 'Operator'){
document.getElementbyId('selectDisable').style.display = "none";
}
else{
document.getElementbyId('selectDisable').style.display = "block";
}
}
</script>
正如你可以看到这个代码真的很容易,我在选择'jabatan'的事件中调用了函数,我将选择'pelajaran'放在'div'中,样式显示为“none”。当调用该函数时,它将获取所选的值,如果值为“运算符”,则不会更改样式...但如果该值为另一个,则会将样式更改为阻止以使“div”可见。
我希望它可以帮到你。再见!