1
Good afternoon, guys! I’m trying to hide a DIV in my code based on the value a field on my site returns.
If its value is contained in "Accident", the DivAcidente
must be displayed. If any other value is returned, it remains hidden.
I tried to do this with Javascript, but the script only runs when I make some changes to this Dropdown, and by default it is already loaded. I’ll leave the specific part of the code, and my JS.
@* Script para controle dos campos de um RTA tipo Acidente *@
<script type="text/javascript">
$(document).ready(function () {
////Chama o evento após selecionar um valor
$('#OrigemAnomalia').on('change', function () {
if (this.value == 'Acidente') {
$(".DivAcidente").show();
}
else {
$(".DivAcidente").hide();
}
});
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
<div class="DivAcidente" style="display:none;">
@Html.LabelFor(model => model.HoraOcorrencia, htmlAttributes: new { @class = "control-label col-md-2", style = "font-size: 14px" })
<div class="col-md-3">
@Html.EditorFor(model => model.HoraOcorrencia, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.HoraOcorrencia, "", new { @class = "text-danger" })
</div>
</div>
@Html.LabelFor(model => model.OrigemAnomalia, htmlAttributes: new { @class = "control-label col-md-2", style = "font-size: 14px" })
<div class="col-md-3">
@Html.DropDownListFor(model => model.OrigemAnomalia, listOrigem, new { @class = "form-control" })
@*@Html.EditorFor(model => model.OrigemAnomalia, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })*@
@Html.ValidationMessageFor(model => model.OrigemAnomalia, "", new { @class = "text-danger" })
</div>
</div>
It’s still not working. Apparently the ID is already assigned with the model name, since this same script works on another page that the user needs to select the option.
– Hudson Medeiros
What is the default value? It may vary? If it is always the same why not also show by default the respective div and only hide if the dropdown changes?
– iamdlm