简体   繁体   English

Bootstrap删除模式内容问题

[英]Bootstrap remove modal content issue

I know that this question is a duplicate, but i can't find a matching answer for my problem. 我知道这个问题是重复的,但是我找不到我的问题的匹配答案。 I am using boostrap 3.2.0 and I have this modal: 我正在使用boostrap 3.2.0,我有这个模式:

<div class="modal fade popup" id="popupSelect">
    <div class="modal-header">
        <a class="close" data-dismiss="modal">&times;</a>
        <h4 class="modal-title">Select meal</h4>
    </div>
    <div class="modal-body-select">
        <div class="form-group">
            <label for="select" class="col-lg-2 control-label">Category</label>
            <div class="col-lg-10">
                <select name="selectCategory" class="form-control selectCategory"
                    id="selectCategory">
                    <option value="0">Select category</option>
                    <c:forEach items="${categories}" var="category">
                        <option value="${category.text}">${category.value}</option>
                    </c:forEach>
                </select>
            </div>
        </div>
        <br>
        <div class="form-group">
            <label for="select" class="col-lg-2 control-label">Meal</label>
            <div class="col-lg-10">
                <select name="selectMeal" class="form-control idMeal" id="selectMeal">
                    <option value="0">Select meal</option>
                </select>
            </div>
        </div>
        <input type="hidden" value="-1" id="hSelectIndex"/>
    </div>

    <div class="modal-footer">
        <a href="" class="btn btn-warning buttons" data-dismiss="modal">Close</a>
        <input type="button" class="btn btn-warning buttons btnSaveChanges"
            value="Save Changes" />
    </div>
</div>

First time when the modal is loaded the content is correct. 第一次加载模态时,内容正确。 After closing the modal, every time the loaded content is the one selected first time. 关闭模态后,每次加载的内容都是第一次选择的内容。 I tried to remove the data content using: 我尝试使用以下方法删除数据内容:

 function removeModalContent(){
    $('#popupSelect').on('hidden', function () {

        $(this).removeData();

    });
}

But it is not working! 但这不起作用! What i am doing wrong? 我做错了什么? 在此处输入图片说明 Every time the select button is pressed the modal is loaded. 每次按下选择按钮时,都会加载模态。 And i need to clear the content every time the modal is "hidden". 而且,每次“隐藏”模态时,我都需要清除内容。

I'm not entirely clear on what your problem is, but first things first, in Bootstrap 3 the event that is fired when a modal is hidden is not hidden , it is hidden.bs.modal (See Bootstrap Docs , under heading 'Events'). 我尚不清楚您的问题是什么,但首先,在Bootstrap 3中,隐藏模式时触发的事件并未hidden ,而是hidden.bs.modal (请参阅Bootstrap Docs ,标题为``事件''下) ')。 So this... 所以这...

$('#popupSelect').on('hidden', function () {...

will not fire. 不会开火。 Change it to... 更改为...

$('#popupSelect').on('hidden.bs.modal', function () {...

I'm also not clear why that is inside a function called 'removeModalContent'. 我也不清楚为什么在名为“ removeModalContent”的函数中。 Really you want to set up the event handler on load. 确实,您想在加载时设置事件处理程序。 What you would have to do in your current case is call that function just to set up the event listener. 在当前情况下,您要做的就是调用该函数以设置事件侦听器。

Like I said, not entirely sure on your particular circumstances/problem so if this doesn't help or I am missing something let me know and I'll have another look. 就像我说的,不确定您的特定情况/问题,因此,如果这没有帮助或我遗漏了一些信息,请告诉我,我会再看看。

When I work with modals I always prefer to clear any user interaction before poping it up. 当我使用模式时,我总是喜欢在弹出之前清除所有用户交互。 This is how I do it: 这是我的方法:

1) Subscribe to button click event in order to show the modal window: 1)订阅按钮单击事件以显示模式窗口:

$("#btnSearchCustomer").click(function () {
        searchCustomers($(this));
    });

2) The function searchCustomers shows the pop-up window. 2)函数searchCustomers显示弹出窗口。

function searchCustomers(btn) {
    btn.attr("disabled", "disabled"); // disables the button to avoid loading data more than once
    clearSearchForm(); // clears any user input
    // checks if the modal has all the data loaded (to avoid loading on start)
    if (!searchForIsLoaded) {
        loadSearchForm(); // loads any necessary data from the DB using AJAX
        searchForIsLoaded = true;
    }
    $('#mdlSearchForm').modal('show'); // shows the modal window
    btn.removeAttr("disabled"); // enables the button again
}

You need to implement the function clearSearchForm for cleaning any field the user has modified. 您需要实现函数clearSearchForm来清除用户已修改的任何字段。

3) Subscribe to OK button on modal window and do something with user input 3)订阅模态窗口上的“确定”按钮,并使用用户输入进行操作

4) Hide modal form 4)隐藏模态形式

I hope it helps 希望对您有所帮助

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

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