javascript - 使用 Javascript 显示 Div 无法在 Select 上工作
问题描述
如果用户在 select 语句中选择了医生值,我将尝试显示 FILE DIV。我知道 if 语句有效,因为我还在控制台中打印了该值,它工作得非常好。我在我的其他网页中以相同的方式切换我的 div,所以我不明白这个网页发生了什么。
function viewFile(){
var file = document.getElementById("doclicense");
var type = document.getElementById('accountType').value;
if(type === 'doctor') {
file.style.display = "block";
console.log(type);
}
}
.hidden{
display : none;
}
<div>
<select id="accountType" name="type" class="form-control" onchange="viewFile()">
<option required>Account Type</option>
<option value="doctor" name="doctor" id="doctor">Doctor</option>
<option value="regular" name="regular" id="reg">Regular Account</option>
</select>
</div>
<div class="hidden file" id="doclicense">
<input type="file" name="license" />
<input type="submit"/>
</div>
******************************************编辑解决方法******** ***************
由于我的代码拒绝工作,我添加了一行代码,标题是“head”,而不是真正的值。感谢所有做出贡献的人。我完全取出了隐藏类,但是当我添加它时,它仍然无法正常工作。
function viewDocFile() {
var file = document.getElementById("doclicense");
var type = document.getElementById('accountType').value;
if (type === 'regular' || type === 'head') {
file.style.display = "none";
console.log(type);
} else {
file.style.display = "block";
console.log(type);
}
}
******************************最终编辑******************** **** 保留原始代码,但添加了内联 CSS。
<div class="form-group col-md-6" id="doclicense" style="display:none;">
现在完美运行。
解决方案
下面是如何编写此代码的示例(即使仍然存在恐怖)
// declare them here and not in a function where they will be redone each time the function is called
const
file_IHM = document.querySelector('#doclicense')
,
type_IHM = document.querySelector('#accountType') // and not with .value ...!
;
type_IHM.onchange = function()
{
file_IHM.style.display = (this.value==='doctor')?"block":"none";
console.log('type_IHM.value', this.value );
}
#doclicense { display : none; }
<div>
<select id="accountType" name="type" class="form-control" > <!-- let the js in the js part -->
<option required>Account Type</option>
<option value="doctor" id="doctor" >Doctor</option>
<option value="regular" id="regular" >Regular Account</option>
</select>
</div>
<div class="file-class" id="doclicense"> <!-- do not use class="hidden... -->
<input type="file" name="license" />
<input type="submit" /> <!-- there is no form anywhere... why don't you use <button> ?? -->
</div>
推荐阅读
- c - 箭头运算符和后增量的优先级?
- stream - 如何解决“无法解析可选双精度中的方法收集”的问题?
- amazon-web-services - 为什么必须在 AWS Direct Connect + VPN 设置中使用公共 VIF?
- javascript - 将文本输入样式化为日期输入
- node.js - 在提供的文件夹中递归读取图像文件
- javascript - 为什么我的浏览器没有打开 websocket 连接?
- php - 使用 PHP 中的 EXEC 函数在 Powershell 中使用 ConvertFrom-Json 获取传入的 PHP 参数(json_eoncoded 数组)时出错
- kubernetes - 是否可以在 Kubernetes 的不同命名空间中拥有超过 1 个角色和 1 个服务帐户?
- java - Java中单链表中addFirst方法的实现返回null?
- expression - 这种语言的表达