提交输入类型时如何触发模态

时间:2018-05-29 02:56:33

标签: html ajax asp.net-mvc modal-dialog

我想在输入类型提交时触发模态,
当我完成我的要求时,需要提交数据然后启动模态样本
(使用ajax更新区域)
但是我尝试在输入类型上添加数据切换,在添加之后,表单似乎无法发布到Controller,是否有人有任何解决方案?

<input type="submit" value="Ok" />
 <button type="button" class="btn btn-primary" data-toggle="modal" data- 
target="#exampleModalCenter">
    launch modal
    </button>

    <!-- Modal -->
    <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLongTitle">Your Content</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body" id="PlanCompleteZone">
                    @Html.Action("PlanComplete")
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
    <!-- Modal -->

1 个答案:

答案 0 :(得分:0)

我建议你在表单中使用onSubmit事件的监听器,这样你就可以在提交时触发javascript函数,在这个函数中你必须阻止事件(提交)并启动你的模态,最后使用ajax请求提交形式内容。

<form method="POST" onsubmit="submit(event);">
    <input type="submit" value="Ok" />
</form>
   <script>
        function submit(event){
           event.preventDefault();
           //assuming bootstrap 3
           $('#exampleModalCenter').modal('show')
           //ajax request
        }
   </script>