我有一個頁面可以將結果添加到表單上的某個區域。我利用編輯器模板將結果分組到區域中,並允許從下拉列表中選擇結果。我想爲三種可能的結果中的兩種顯示可選評論框。編輯器模板上的Javascript
主要頁面如下:
@model DBS.ViewModels.OutcomeQuestionnaireVM
@{
ViewBag.Title = "Outcomes";
}
<h2>Add Outcomes</h2>
@if (Model.Error == true)
{
<h3 class="danger">You MUST select an outcome for at least 1 area.</h3>
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.HiddenFor(x => x.DebriefId)
<hr />
@Html.EditorFor(m => m.Groups, new { outcomes = Model.Outcomes })
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Next" class="btn btn-default" />
</div>
</div>
</div>
}
組編輯器模板是:
@model DBS.ViewModels.OutcomeQuestionGroupVM
@{
Layout = null;
}
<h4>@Html.DisplayFor(m => m.Name)</h4>
@Html.EditorFor(m => m.Questions, new { outcomes = ViewData["outcomes"] })
爲結局的最後編輯模板是:
@model DBS.ViewModels.OutcomeQuestionVM
@{
Layout = null;
}
<div class="form-group">
<div class="row">
<div class="col-md-4">
@Html.DisplayFor(m => m.Question)
</div>
<div class="col-md-4">
@Html.HiddenFor(m => m.ID)
@Html.DropDownListFor(m => m.OutcomeId, (SelectList)ViewData["outcomes"], "Please Select if applicable", new { @class = "form-control", @id = "OutcomeId" })
@Html.ValidationMessageFor(m => m.OutcomeId, "", new { @class = "text-danger" })
</div>
<div class="col-md-4" id="Comments">
@Html.HiddenFor(m => m.Comments)
@Html.TextAreaFor(model => Model.Comments, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Comments, "", new { @class = "text- danger" })
</div>
</div>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(document).ready(function()
{
document.getElementById('Comments').hide;
});
$(function() {
$(".DropChange").change(function() {
var valone = $('#OutcomeId').val();
if (valone == 1 || valone == 2)
{
$('#Comments').show();
}
else if (valone == 3)
{
$('#Comments').hide();
}
else
{
$('#Comments').hide();
}
});
});
</script>
}
但是JavaScript的沒有按什麼都不做。
我添加了這段代碼,但是它在var comment = $(this).closest('row')上的錯誤。行,有一個拼寫錯誤,我已經糾正,但它仍然無法找到相應的評論框類 –
對不起 - 我錯過了'.'(行類選擇器) - 請參閱編輯 –
請參閱[這個小提琴](https:// jsfiddle .net/8ehot3ft /)爲例 –