我也閱讀了許多關于堆疊溢位的教程和許多答案,但它仍然對我不起作用。從下拉串列中選擇一個選項(是)后 - >它應該顯示<div>標簽,否則<div>應該隱藏。
下面是我的代碼:
<h4>1. Does your school participate in the sponsored CAT4 scoring initiative?</h4>
@{
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem
{
Text = "Yes",
Value = "True"
});
listItems.Add(new SelectListItem
{
Text = "No",
Value = "False"
});
}
<div class="form-group" id="PickOption">
<div class="col-md-10">
@Html.DropDownListFor(model => model.ParticipateInCAT4Scoring, listItems, "<----Select Yes or No---->", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ParticipateInCAT4Scoring, "", new { @class = "text-danger" })
</div>
</div>
<div id="OnYes" class="form-group" style="display:none">
@Html.Label("For what Grade levels?", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(m => m.GradeLevelsCAT4, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(m => m.GradeLevelsCAT4, "", new { @class = "text-danger" })
</div>
</div>
下面是我放置在視圖頁面底部的腳本檔案:
@section Scripts{
<script type="text/javascript">
$(document).ready(function () {
$('#PickOption').on('change', function () {
if ($(this).val() == 'True') {
document.getElementById('OnYes').style.display = "";
}
else {
document.getElementById('OnYes').style.display = "none";
}
});
});
</script>
}
uj5u.com熱心網友回復:
PickOption是包裝 div 的 ID。
將此行替換#PickOption為選擇框的 ID 或選擇器(通常是自動生成的,但您可以指定。
$('#PickOption').on('change', function () {
另外,由于您使用的是 jQuery,因此您可以這樣做$('#OnYes').show();
https://api.jquery.com/show/
uj5u.com熱心網友回復:
您的選擇器不正確,請確保您在下拉串列中系結事件,以便在下拉串列中添加一個類
@Html.DropDownListFor(model => model.ParticipateInCAT4Scoring, listItems, "<----Select Yes or No---->", new { @class = "form-control pickerDropdown" })
現在將事件系結到添加的類上,如下所示:
$('.pickerDropdown').on('change', function () {
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/459418.html
標籤:javascript
