【问题标题】:Show modal on drop down list selection在下拉列表选择中显示模式
【发布时间】:2016-04-13 18:23:43
【问题描述】:

我有一个包含下拉列表的模式,在选择/onchange 事件时,我希望新视图显示在模式中。我已经使用this solution 来实现多个模态叠加,并尝试将 javascript 从 onclick 更改为 on change 但没有运气

我的部分观点

@using (Html.BeginForm("GameAssignerTable", "Admin", FormMethod.Post, new {role = "form"}))
{
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span></button>
        <h3 class="modal-title">Select Investigator Group</h3>
    </div>
    <div class="modal-body">
        @Html.DropDownListFor(m => m.SelectedGroupUserId, Model.GroupList, "Select Investigator Group", new { @class = "form-control modal-link3", onchange = "this.form.submit();" })
    </div>
}

我的布局页面

  <div class="container">
    <div class="row">
        <div id="modal-container3" class="modal fade" tabindex="-1" role="dialog">
            <div class="modal-dialog">
                <div class="modal-content col-sm-8 col-lg-offset-2">
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
        $(document).on('show.bs.modal', '.modal', function() {
            var zIndex = 1040 + (10 * $('.modal:visible').length);
            $(this).css('z-index', zIndex);
            setTimeout(function() {
                $('.modal-backdrop').not('.modal-stack').css('z-index', zIndex - 1).addClass('modal-stack');
            }, 0);
        });

        // Initialize popover
        $(function() {
            $('[data-toggle="popover"]').popover({ html: true });
        });

        // Used for modals
        $(function() {
            // Initialize modal dialog
            // attach modal-container bootstrap attributes to links with .modal-link class.
            // when a link is clicked with these attributes, bootstrap will display the href content in a modal dialog.

            $('body').on('change', '.modal-link3', function (e) {
                e.preventDefault();
                $(this).attr('data-target', '#modal-container3');
                $(this).attr('data-toggle', 'modal');
            });

            // Attach listener to .modal-close-btn's so that when the button is pressed the modal dialog disappears
            $('body').on('click', '.modal-close-btn', function() {
                $('#modal-container').modal('hide');
            });
            //clear modal cache, so that new content can be loaded
            $('#modal-container3').on('hidden.bs.modal', function() {
                $(this).removeData('bs.modal');
            });
            $('#CancelModal').on('click', function() {
                return false;
            });
        });

    </script>

【问题讨论】:

    标签: javascript jquery asp.net-mvc twitter-bootstrap bootstrap-modal


    【解决方案1】:

    我建议将 JS 拉出到其他处理程序中,然后执行以下操作:

    $("#@Html.IdFor(m => m.SelectedGroupUserId)").on("change", function(e) { 
        $(this).closest("form").trigger("submit");
    });
    

    请注意,这会提交表单,然后发回操​​作。然后该操作将返回某种视图以显示结果。您指出了另一个模式,但同步回发会让您远离当前视图。

    【讨论】:

    • 这只是提交表单的另一种方式。提交页面后,我将如何加载模式?
    • 一旦提交,你可以触发一个modal来展示,但是记住,当前页面被销毁(从客户端的角度来看),当你进入新页面时,你必须找到一种方法来触发它(就像通过模型将标志传递给视图,最终调用 JavaScript modal("show");)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2014-02-15
    • 2012-11-15
    相关资源
    最近更新 更多