javascript - 根据使用 javascript 在 Asp.net 中的下拉选定值显示和隐藏 div 不起作用
问题描述
我有三个隐藏的 div 我必须根据下拉菜单的选择显示它们但它不起作用我在做什么错了?
我的下拉菜单
<asp:DropDownList ID="ddlPermissionMode" runat="server" Width="100%" Height="25px">
<asp:ListItem>choose</asp:ListItem>
<asp:ListItem>Individual</asp:ListItem>
<asp:ListItem>Group</asp:ListItem>
</asp:DropDownList>
我的 div
<div class="choose box row text-center">
Select A permission Mode if you want to give This user a <strong>permission</strong>
</div>
<div class="Individual row box">
l
</div>
<div class="Group row box">
You have selected <strong>blue option</strong> so i am here
</div>
脚本
<%-- <script>
$(document).ready(function(){
$("#ddlPermissionMode").change(function(){
$(this).find("option:selectedValue").each(function(){
var optionValue = $(this).attr("value");
if(optionValue){
$(".box").not("." + optionValue).hide();
$("." + optionValue).show();
} else{
$(".box").hide();
}
});
}).change();
});
</script>
风格
<style>
.box{
color: #fff;
display: none;
margin-top: 20px;
}
.choose{ background: #ff0000; }
.Individual{ background: #228B22; }
.Group{ background: #0000ff; }
</style>--%>
解决方案
您需要更改$(this).find("option:selectedValue")
为$(this).find("option:selected")
一切似乎都很好,只需像这样更改您的脚本
<%-- <script>
$(document).ready(function(){
$("#ddlPermissionMode").change(function(){
$(this).find("option:selected").each(function(){
var optionValue = $(this).attr("value");
if(optionValue){
$(".box").not("." + optionValue).hide();
$("." + optionValue).show();
} else{
$(".box").hide();
}
});
}).change();
});
</script>
推荐阅读
- javascript - 使用js创建异步循环
- java - 将数据放入 List 然后将其值插入数据库
- oracle - 来自触发器的 PL SQL 过程
- azure - 在 Azure 数据工厂中创建按需 hadoop 集群失败;附加属性未激活
- regex - 使用 htaccess,你如何重写超过 8 个字符或更多字符的参数?
- angular - 使用 NgModel 基于绑定变量更改输入字段中显示的值
- python - 库仑定律的 Runge-Kutta 方法的问题
- dart - 如何在flutter中通过webview下载/创建pdf
- python - 如何使用我自己的函数 bin_add 添加列表元素
- php - PHP - 将以字符串格式提供的信息转换为数组格式