前言
在维护一个旧项目的时候,负责人让我写一个功能:通过切换下拉框隐藏某一模块,我想了下貌似很简单,就说可以,结果就踩了一些坑
先看html部分代码
<select name="type" class="input w50 inform" onchange="dd()">
<option value="通知类">通知类</option>
<option value="验证码类">验证码类</option>
</select>
<div class="form-group note">
//这是要隐藏的模块
</div>
获取select的值得话有两种方法,一种是没有设置value,获取方式是
document.querySelector(".xx").val
设置了value的话就是
document.querySelector(".xx")value
下面是实现的具体方法
let inform = document.querySelector(".inform");
let note = document.querySelector(".note");
note.style.display = "none"
function dd() {
var type = inform.value
if (type == "通知类") {
note.style.display = "none"
} else if (type == "验证码类") {
// console.log(type);
note.style.display = "block"
// console.log(inform.value);
}
}