2

I am trying to hide and show div in my mvc 5 project using dropdownlist change event, i have researched, and luckily i found this code online, but it doesn't seem to work for me, i will appreciate if anyone could point at where i am making mistakes. Thanks in advance.

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $(document).ready(function () {
            $("#CountryID").change(function () {
                if ($(this).val() == "Ghana") {
                    $("#showStateLga").show();
                    $("#showStateLgaText").hide();
                } else {
                    $("#showStateLga").hide();
                    $("#showStateLgaText").show();
                } 
            });
        });
    });
</script>

Dropdownlist control:

<div class="form-group">
                @Html.LabelFor(model => model.CountryID, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.CountryID, (IEnumerable<SelectListItem>)ViewBag.cCountryList, "---Select---", new {@class = "form-control" })
                    @Html.ValidationMessageFor(model => model.CountryID, "", new { @class = "text-danger" })
                </div>
            </div>

Div Control:

 <div id="showStateLga" style="display: none">
                <div class="form-group">
                    @Html.LabelFor(model => model.notState, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.notState, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.notState, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.notCity, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.notCity, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.notCity, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>

Rendering Results:

Dropdownlist:

    <div class="form-group">
                    <label class="control-label col-md-2" for="CountryID">Country:</label>
                    <div class="col-md-10">
                        <select class="form-control" data-val="true" data-val-number="The field Country: must be a number." data-val-required="Select country" id="CountryID" name="CountryID"><option value="">---Select---</option>
                   <option value="1">Afghanistan</option>
                   <option value="2">Ghana</option>
                   <option value="3">Albania</option>
                   <option value="4">Algeria</option>
                       </select>
                        <span class="field-validation-valid text-danger" data-valmsg-for="CountryID" data-valmsg-replace="true"></span>
                    </div>
                </div>

Div1:

    <div id="showStateLga" style="display:block">

                    <div class="form-group">
                        <label class="control-label col-md-2" for="UserStateList">State:</label>
                        <div class="col-md-10">
                            <select class="form-control" id="State" name="State"><option value="">---Select State---</option>
                          <option value="1">Abia State</option>
                         <option value="2">Adamawa State</option>
                          <option value="3">Akwa Ibom State</option>

                        </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-2"              for="UserCity">City:</label>
                        <div class="col-md-10">
                            <select id="lga" name="lga" class="form-control"    required>
                                <option value="">---Select LGA---</option>
                            </select>
                        </div>
                    </div>
                    </div>

Div2:

<div id="showStateLgaText" style="display:none">
                    <div class="form-group">
                        <label class="control-label col-md-2" for="notNigeriaState">State:</label>
                        <div class="col-md-10">
                            <input class="form-control text-box single-line" data-val="true" data-val-required="Enter state" id="notNigeriaState" name="notNigeriaState" type="text" value="" />
                            <span class="field-validation-valid text-danger" data-valmsg-for="notNigeriaState" data-valmsg-replace="true"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-2" for="notNigeriaCity">City:</label>
                        <div class="col-md-10">
                            <input class="form-control text-box single-line" data-val="true" data-val-required="Enter city" id="notNigeriaCity" name="notNigeriaCity" type="text" value="" />
                            <span class="field-validation-valid text-danger" data-valmsg-for="notNigeriaCity" data-valmsg-replace="true"></span>
                        </div>
                    </div>
                </div>
0

1 Answer 1

3

I found the problem. That is the value of $("#CountryID") is CountryID instead of CountryName.

$(function () {
    $(document).ready(function() {
        $("#CountryID").change(function () {
            if ($(this).val() != "Ghana") { // It doesn't work over here.
                $("#showStateLga").show();
            } else {
                $("#showStateLga").hide();
            }
        });
    });
});

There are 2 ways to fix it. First

if ($(this).val() != "2") { // Replace the match text to CountryID.

Or

if ($(this).find(':selected').text() != "Ghana") { // Replace .val() to .find(':selected').text().
Sign up to request clarification or add additional context in comments.

9 Comments

thanks for your reply, only the .show() part that works now, after showing, the div refused to be hidden even. please is there any other thing i can do?
Could you give me the rendering results of both Dropdownlist control and Div Control parts?
I don't know what you mean by rendering result.
Generally, the codes prepended with @html are server-side program. So server will run the program and generate a perfect html file to browser. Anyway, rendering result means html source. By the way, why I need the result is the possible reason might be a UI issue.
The rendering result is too long to add as comment even when i have removed some part, i don't know how to send that to you now,
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.