javascript - 如何在 javascript 中使用 this.value 从下拉列表中检查特定值
问题描述
我有一个下拉选择列表,其中大约有 10 个选项与一个隐藏的 div 连接,当使用 javascript 代码仅选择特定 3 个选项中的任何一个时应该显示
document.getElementById('item').addEventListener('change', function () {
var style = (this.value == "661056067" or this.value == "571855424") ?
'table-row' : 'none';
document.getElementById('hidden_div').style.display = style;
});
我尝试了上面显示的代码,因为我希望 this.value 函数等于多个值,但它不起作用。那么让ot工作的正确方法是什么。请注意,我一点也不擅长 javascript。谢谢你的帮助
解决方案
而不是or
使用||
document.getElementById('item').addEventListener('change', function () {
var style = (this.value == "661056067" || this.value == "571855424") ?
'table-row' : 'none';
document.getElementById('hidden_div').style.display = style;
});
您也可以使用includes
.
document.getElementById('item').addEventListener('change', function () {
var style = ["661056067", "571855424"].includes(this.value) ? 'table-row' : 'none';
document.getElementById('hidden_div').style.display = style;
});
推荐阅读
- mysql - 如何阻止创建名称与特定模式匹配的 MySQL 数据库
- android - 无效的可绘制添加到 LayerDrawable!Drawable 已经属于另一个所有者,但没有公开一个常量状态
- node.js - MongoDB集合中数组的自定义查询
- powershell - 在 PowerShell 中,您可以写入托管外部命令的调用和参数吗?
- amazon-web-services - 胶水性能?
- python - 无法从“sklearn.metrics”导入名称“mean_absolute_percentage_error”
- spring-boot - Spring Cloud - 如何动态获取服务的主机和端口
- ios - 我不能用 cocoapod 安装包
- django - 为什么我的带有recaptcha 的django 表单发送数据甚至清空recaptcha?
- cmake - Cmake 创建 Makefile 支持不同配置的调试和发布