【发布时间】:2016-06-01 09:26:25
【问题描述】:
我有一个包含 7 个选项的下拉列表。用户选择的每个选项都必须在其正下方的文本区域中填写描述。我正在尝试实现的是选择一个选项,显示适当的div并隐藏其他div。这就是我目前所拥有的
$(document).ready(function () {
$("#define-labelling").hide();
... hide the remaining divs here
$("#ProjectStatus").change(function () {
var selectedValue = "";
$("#ProjectStatus option:selected").each(function () {
selectedValue += $(this).val();
if (selectedValue === "Define (Labelling)") {
$("#define-labelling").fadeIn("slow");
}
... More if statements here for the other divs
});
});
});
});
我的问题是,有没有更清洁的方法来解决这个问题?因为目前我有多个 if 语句并且隐藏和显示适当的 div 变得有点难看。
在@Mairaj 回答之后更新
function showDiv() {
var divID = $("#ProjectStatus option:selected").attr("data-div");
alert(divID); //this comes as undefined
$("#" + divID).show();
$("#" + divID).siblings().hide();
}
我添加了两个 div:
<div class="form-group" id="Define (Labelling)" style="display: none;">
@Html.LabelFor(model => Model.LabellingInfo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => Model.LabellingInfo, htmlAttributes: new { @class = "form-control description" })
</div>
</div>
<div class="form-group" id="Analysis (Root Cause)" style="display:none;">
@Html.LabelFor(model => Model.RootCauseInfo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => Model.RootCauseInfo, htmlAttributes: new { @class = "form-control description" })
</div>
【问题讨论】:
-
也许你可以添加一个类
none和.none{display: none;},如果发生变化然后$(#ProjectStatus).addClass('none')和$("#define-labelling")..removeClass('none') -
@gdreamlend 你能详细说明一下吗?
-
您在选择选项中显示的内容是否已修复。
-
@MickeyPatel 从数据库中填充的值,它们不会改变
标签: jquery html asp.net-mvc