Ich habe eine Seite, um Ergebnisse zu einem Bereich in einem Formular hinzuzufügen. Ich benutze Editor-Vorlagen, um die Ergebnisse in Bereiche zu gruppieren und das Ergebnis aus einer Dropdown-Liste auszuwählen. Ich möchte ein optionales Kommentarfeld für zwei der drei möglichen Ergebnisse zeigen.Javascript auf Editor-Vorlage
Die Hauptseite ist die folgende:
@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>
}
Der Gruppeneditor Vorlage:
@model DBS.ViewModels.OutcomeQuestionGroupVM
@{
Layout = null;
}
<h4>@Html.DisplayFor(m => m.Name)</h4>
@Html.EditorFor(m => m.Questions, new { outcomes = ViewData["outcomes"] })
Die endgültige Template-Editor für das Ergebnis ist:
@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>
}
jedoch JavaScript doesn tu nichts.
Ich habe diesen Code aber seine Fehler auf der var comment = $ (this) .close ('row'). find ('. Kommentare'); Linie, es gab einen Tippfehler, den ich berichtigt habe, aber es kann immer noch nicht das entsprechende Kommentarfeld nach Klasse finden –
Sorry - Ich habe das '.' (Klassenselektor für Zeile) verpasst - siehe Edit –
Siehe [this fiddle] (https: // jsfiddle .net/8ehot3ft /) für ein Beispiel –