不要在不刷新的情况下一次获取弹出表单

Don't get the pop-up form more the one time without refreshing

提问人:Shahriar 提问时间:10/4/2023 更新时间:10/4/2023 访问量:22

问:

<!-- Modal -->
 <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                @* <h5 class="modal-title" id="exampleModalLabel">Modal title</h5> *@
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <--- My Form Loads Here ---->
                <div class="modal-body" id="partial_view_div">
                </div>
            </div>
        </div>
    </div>
</div>
<div>
    <button id="CreateAirportForm">Create New</button>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $('body').on('click', '#CreateAirportForm', function (event) {
        LoadNewAirport();
    });
    function LoadNewAirport() { 
        $('#exampleModal').modal('show'); 
        $.ajax({
            type: "GET",
            url: '@Url.Action("LoadNewAirportForm", "Airports")',
            data: {},
            success: function (data) {
                $('#partial_view_div').html(data); 
                
            },
            error: function (xhr, ajaxOption, thrownError) {
                alert("An error occurred while loading the partial view.");
            }
        });
    } 
</script> 

第一次按下“新建”按钮后,弹出表单工作正常。但是第二次它不起作用。如果我刷新我的页面,那么它第一次可以正常工作,但第二次就不起作用了。

我怎样才能解决这个问题。我想在按下“新建”按钮时多次获取弹出表单。

jquery ajax asp.net-mvc 引导-5

评论

0赞 john Smith 10/4/2023
我真的看不出代码中有任何问题,还是涉及更多代码?你能制作一个重现问题的代码片段吗?
0赞 freedomn-m 10/4/2023
提示:约定是你的元素与它们是什么 - 暗示idCreateAirportForm <form id=CreateAirportForm>
0赞 freedomn-m 10/4/2023
第二个提示:在加载占位符的情况下显示您的模态,或者在 AJAX 完成之前不显示您的模态 - 否则,您会在加载新内容时短暂显示旧模态
0赞 freedomn-m 10/4/2023
假设您只有一个通用名称,那么问题很可能出在您未提供的代码中 - 即当您从弹出窗口中保存时,或者更具体地说,关闭模态时。很可能你正在破坏模态,而不仅仅是隐藏它。<div id=partial_view_div>
1赞 freedomn-m 10/4/2023
在测试页面时添加并检查控制台之前 - 第一次单击这应该是 1,第二次单击(关闭模式后)它可能是 0。即调试你的代码,不要立即跳到“它不起作用”$('#exampleModal').modal('show'); console.log($('[id=exampleModal]').length)

答: 暂无答案