简体   繁体   English

jQuery模式自动关闭

[英]Jquery modal auto close

I have this jquery code 我有这个jQuery代码

$(document).ready(function () {
$('.btn.btn-primary.patientId').on('click',
            function (e) {
                var $this = $(e.target),
                    $tr = $this.closest("tr");
                var cid = $tr.find(".hdncid").eq(0).val();
                var search;
                    eLoader(true);
                loadUsers('/Companies/AssociateWithCaregiver', true);

    function loadUsers(url, addData) {
        $.get({
            url: url,
            data: addData ? { id: cid, search: search } : {}
        }).done(function (e) {
            eLoader(false);   
            $("#listOfCaregivers .modal-body").html(e);
            $('.btn.btn-primary.submit').click(function () {
                $("#searchForm").submit();
                search = $('#search').val();
                loadUsers('/Companies/AssociateWithCaregiver', true);
            });
            $('.btn.btn-primary.addcaregiver').click(function (b) {
                var $thiscaregiver = $(b.target),
                    $tr = $thiscaregiver.closest("tr");
                var caregiverid = $tr.find(".cgcid").eq(0).val();
                $.post("/Companies/Associate", { patientId: cid, caregiverId: caregiverid });
                $('.close').click();
            });
            $('.btn.btn-default.btn_delete.removecaregiver').click(function (b) {
                var $thiscaregiver = $(b.target),
                    $tr = $thiscaregiver.closest("tr");
                var caregiverid = $tr.find(".cgcid").eq(0).val();
                $.post("/Companies/AssociateRemove", { patientId: cid, caregiverId: caregiverid });
                $('.close').click();
            });

        }).fail(function (e) {
            eLoader(false);

Button <form id="searchForm"> <div class="patientsearch" style="padding-left: 311px;"> <div class="input-group" style="width: 250px;"> @Html.TextBox("search", "", new { placeholder = "Search Caregivers", @class = "form-control" }) <span class="input-group-btn"> <button type="submit" class="btn btn-primary submit">Search</button> </span> </div> </div> </form> 按钮<form id="searchForm"> <div class="patientsearch" style="padding-left: 311px;"> <div class="input-group" style="width: 250px;"> @Html.TextBox("search", "", new { placeholder = "Search Caregivers", @class = "form-control" }) <span class="input-group-btn"> <button type="submit" class="btn btn-primary submit">Search</button> </span> </div> </div> </form>

Problem is that when I opening modal window - geting data and want to search I click Search (.btn.btn-primary.submit) and then I received filtered data But in few second modal window automatically closes. 问题是,当我打开模式窗口-获取数据并要搜索时,我单击搜索(.btn.btn-primary.submit),然后接收到过滤后的数据,但是在第二个模式窗口中自动关闭。 And after this I can see in the address bar my link + ?search='my search request'. 之后,我可以在地址栏中看到我的链接+?search ='我的搜索请求'。

What's wrong in my code? 我的代码有什么问题? Help me please. 请帮帮我。 Thank you. 谢谢。

The default behaviour of clicking a button will submit the form which is why your modal closes and the address changes. 单击按钮的默认行为将提交表单,这就是您的模式关闭和地址更改的原因。

There are two ways around this, you can add a type="button" attribute to your button or you can add a listener for your form and return false to prevent the submit. 有两种解决方法,可以向type="button"添加type="button"属性,也可以为表单添加侦听器并返回false以防止提交。

eg $('#your_form').submit(function() { return false; }); 例如$('#your_form').submit(function() { return false; });

can you try e.preventDefault() to prevent form submission before ajax request to keep model open 您可以尝试使用e.preventDefault()来防止在ajax请求保持模型打开之前提交表单吗?

$('.btn.btn-primary.submit').click(function (e) {
        e.preventDefault();
        //$("#searchForm").submit();
        search = $('#search').val();
        loadUsers('/Companies/AssociateWithCaregiver', true);
});

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

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