gpt4 book ai didi

javascript - 根据 DropDownListFor 显示/隐藏 div

转载 作者:行者123 更新时间:2023-11-28 13:07:51 24 4
gpt4 key购买 nike

我需要更改 2 div 的可见性方面的帮助根据 DropDownListFor 中所选值的元素。

比方说,dropDown 中有值“One”、“Two”和“Three”。

  • 我想显示 div id="time"当选择“一”或“二”的值并隐藏时 div id="fromTo"
  • 当选择值“三”时,我想显示 div id="fromTo"并隐藏div id="time"

这是我目前拥有的代码:

<div class="form-group">
@Html.LabelFor(model => model.I_Uloha, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.UlohaString, (SelectList)ViewBag.Tasks, new { @class = "form-control"})
@Html.ValidationMessageFor(model => model.I_Uloha, "", new { @class = "text-danger" })
</div>
</div>


<div id="time" class="form-group">
@Html.LabelFor(model => model.Zataz, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Zataz, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Zataz, "", new { @class = "text-danger" })
</div>
</div>

<div id="fromTo">
<div class="form-group">
@Html.LabelFor(model => model.D_Zac, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.D_Zac, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.D_Zac, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.D_Kon, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.D_Kon, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.D_Kon, "", new { @class = "text-danger" })
</div>
</div>
</div>

最佳答案

您需要使用 jQuery show()hide() 方法来更改 View 页面中 div 元素的可见性,具体取决于下拉菜单列出这样的值:

<script type="text/javascript">
$(document).ready(function () {
$('#UlohaString').change(function () {
var ddlValue = $(this).val();

if (ddlValue == "One" || ddlValue == "Two")
{
// show time div, hide fromTo div
$('#time').show();
$('#fromTo').hide();
}
else if (ddlValue == "Three")
{
// show fromTo div, hide time div
$('#fromTo').show();
$('#time').hide();
}
});
});
</script>

请注意,如果$(this).val(),则可以使用var ddlValue = $(this).find(':selected').text()在这里不起作用。

引用:

How to hide and show div in asp.net mvc 5 using dropdownlist change event

关于javascript - 根据 DropDownListFor 显示/隐藏 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45207169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com