Hide/Display a input field based on value from HtmlRadioButtonFor and make it mandatory if display

自作多情 提交于 2020-03-04 21:34:06

问题


I have a scenario where I have two different div's and the later should disappear based on boolean value from earlier div. if the radiobutton value is true, later div should be a required filed.

Actual Code:

<div class= row ">
<div class="row col-lg-offset-2 top-buffer">
    <h4><strong><span>Is Employment Offered?</span> <span class="text-danger">*</span></strong></h4>
        @Html.RadioButtonFor(model => model.IsOffered, true) @Html.Label("Yes")
        @Html.RadioButtonFor(model => model.IsOffered, false) @Html.Label("No")
        <br>
</div>
<div class="row col-lg-offset-2 top-buffer" id="employeeNumber">
    <div class="col-sm-2"><b>Enter Employee Number</b></div>
        <div class="col-sm-10">
                @Html.TextBoxFor(model => model.EmployeeNumber, new { @class = "form-control" })
            <br />
        </div>
    </div>
</div>

<script>
$("#employeeNumber").hide();
$("input[name='IsOffered']").on("change", function() {
    if ($(this).val() == "true") {
        $("#employeeNumber").show();
    }
});

I tried above, but no luck. Thanks you for your time!


回答1:


Change it to "True":

$("#employeeNumber").hide();
$("input[name='IsOffered']").on("change", function () {
    if ($(this).val() === "True") {
        $("#employeeNumber").show();
    } else {
        $("#employeeNumber").hide();
    }
});

Because Boolean.ToString() returs "True" instead of "true". So this value is added to the radio that is generated by razor.

Why?



来源:https://stackoverflow.com/questions/46181191/hide-display-a-input-field-based-on-value-from-htmlradiobuttonfor-and-make-it-ma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!