简体   繁体   English

显示和隐藏基于下拉列表的asp mvc 5

[英]show and hide based on drop down list asp mvc 5

I am learning asp.net mvc 5. 我正在学习asp.net mvc 5。

My dropdownlistfor is working perfect and shows right field based on its value. 我的dropdownlistfor工作完美,并根据其值显示正确的字段。 But the problem is when the page loaded first time it shows all field.. 但是问题是页面第一次加载时显示所有字段。

Problem is: I want default is showing nothing when the page first time loaded. 问题是:第一次加载页面时,我希望默认不显示任何内容。

my cshtml: 我的cshtml:

<div class="form-group">
                    <div class="col-md-10">
                        @Html.DropDownListFor(m => m.PaymentMethod, ViewBag.PayTypeList as List<SelectListItem>, new { @class = "btn btn-primary btn-lg dropdown-toggle", @id = "PaymentId" })
                        <div id="PaypalButton">
                            <br/>
                            <script src="https://www.paypalobjects.com/api/button.js?"
                                    data-merchant="braintree"
                                    data-id="paypal-button"
                                    data-button="checkout"
                                    data-color="gold"
                                    data-size="medium"
                                    data-shape="pill"
                                    data-button_disabled="true">
                            </script>
                            <!--data-button_type="paypal_submit"-->
                        </div>
                        <div id="EcheckForm">
                            <div class="form-group">
                                <div class="col-md-10">
                                    @Html.TextBoxFor(m => m.VecInsNum, new { @class = "form-control input-lg", placeholder = "Vehicle Isurance Number", required = "required", tabindex = 18 })
                                    @Html.ValidationMessageFor(m => m.VecInsNum, null, new { @class = "text-danger" })
                                </div>
                            </div>
                        </div>
                    </div>

And Js: 和Js:

@section Scripts {
<script type="text/javascript">
    $(document).ready(function () {
        $('#PaymentId').change(function () {
            var value = $(this).val();
            if (value == '1') {
                $('#PaypalButton').show();
                $('#EcheckForm').hide();
            } else if (value == '2') {
                $('#PaypalButton').hide();
                $('#EcheckForm').show();
            } else {
                $('#PaypalButton').hide();
                $('#EcheckForm').hide();
            }
        });
    });

When the page first time loaded: 首次加载页面时: 在此处输入图片说明

When I select Paypal: 当我选择贝宝时: 在此处输入图片说明

when I select E-check: 当我选择电子支票时: 在此处输入图片说明

When I go back to Default: 当我回到默认值时: 在此处输入图片说明

First, create a function which contains hide methods inside it: 首先,创建一个函数,其中包含隐藏方法:

// hide all div options
function hideOnLoad() {
    $('#PaypalButton').hide();
    $('#EcheckForm').hide();
}

Then, call function above to hide all option div elements when page has loaded at first time and when default value being selected as given below: 然后,在第一次加载页面时以及如下所示选择默认值时,调用上述函数以隐藏所有选项div元素:

$(document).ready(function () {
    hideOnLoad(); // add this line
    $('#PaymentId').change(function () {
        var value = $(this).val();
        if (value == '1') {
            $('#PaypalButton').show();
            $('#EcheckForm').hide();
        } else if (value == '2') {
            $('#PaypalButton').hide();
            $('#EcheckForm').show();
        } else {
            hideOnLoad();
        }
    });
});

Simplified example: JSFiddle Demonstration 简化示例: JSFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM