简体   繁体   English

JQuery UI 对话框模式表单缓存 AJAX 应用程序中的旧值

[英]JQuery UI dialog modal form caching old values in AJAX application

I am having a major headache using a form in a modal dialog with JQuery UI.在带有 JQuery UI 的模态对话框中使用表单时,我感到非常头疼。 The dialog is displayed when the user clicks on a link.当用户单击链接时,将显示该对话框。 The first time the form is opened, it works fine;第一次打开表单时,它工作正常; the form is submitted to the server, the containing page is updated via AJAX, and the dialog closes.表单提交到服务器,包含页面通过 AJAX 更新,对话框关闭。 However, subsequent attempts to use the form are where the problems start.但是,随后尝试使用该表单是问题开始的地方。

On the second attempt to use it, the form is submitted with the values from the previous submission, despite the page content being updated.在第二次尝试使用它时,尽管页面内容正在更新,但仍会使用上次提交的值提交表单。 This happens even if I navigate to a different screen in the application and then return.即使我导航到应用程序中的其他屏幕然后返回,也会发生这种情况。 The content of the page which is re-rendered after each submission includes the form which makes up the dialog, so the 'old' values which are being submitted no longer even exist in the page markup.每次提交后重新呈现的页面内容包括构成对话框的表单,因此正在提交的“旧”值甚至不再存在于页面标记中。 They are being somehow 'remembered' by JQuery UI. JQuery UI 以某种方式“记住”了它们。 I don't know how to work around this behaviour and it's driving me crazy.我不知道如何解决这种行为,这让我发疯。

How can I make the JQuery dialog forget about previous form submissions when the content is updated?如何让 JQuery 对话框在内容更新时忘记以前的表单提交?

I am using JQuery UI 1.7.2 with JQuery 1.3.2, and the application is built in ASP.NET MVC 3 (although I don't believe there is any problem server-side).我正在使用 JQuery UI 1.7.2 和 JQuery 1.3.2,并且应用程序内置在 ASP.NET MVC 3 中(尽管我不相信服务器端) I would prefer not to have to update the version of JQuery I'm using.我宁愿不必更新我正在使用的 JQuery 的版本。

Relevant JavaScript:相关JavaScript:

//shows dialog containing form; triggered by clicking a hyperlink
function showForm() {    
    $('#divForm').dialog({
        modal: true,
        draggable: false,
        resizable: false,
        title: summary,
        width: 400
    });
}

//submits the form; triggered by clicking 'Save' button from the dialog
function save() {
    var postData = $('#frmAddNew').serialize();

    $.post('/Item/Save', postData, function (response, status) {
            $('#divForm').dialog('destroy');
            $('#divContent').html(response); //response mark up contains a new 'divForm' element
        }, 'html');
    }
}

Here is the markup of the form and it's containing div.这是表单的标记,它包含 div。 This is what is reloaded into the page after submission (a PartialViewResult) with the new values for txtDate, txtTime and hdnId.这是提交后重新加载到页面中的内容(PartialViewResult),其中包含 txtDate、txtTime 和 hdnId 的新值。 These are the inputs which are having their old values retained.这些是保留其旧值的输入。

<div id="divForm" class="admPanel hidden">
<form id="frmAddNew" action="/Item/Save" method="post">
    <input id="hdnId" name="UserId" type="hidden" value="3">
    <div>
        <label for="txtDate">Admin Date:</label>
        <input id="txtDate" name="AdministrationDateTime" type="text" value="8 Jun 2011">
        <br>
        <label for="txtTime">Admin Time:</label>
        <input id="txtTime" name="AdministrationDateTime.Time" type="text" value="21:45">
        <br>
        <label>Outcome:</label>
    <input id="txtOutcome" name="AdministrationOutcome" type="text" value="">
        <br>
    </div>
</form>
<p>
    <input class="buttonNormal" id="btnSave" onclick="save();" type="button" value="Save">
</p>

divContent is an empty div which lives in a master page and is loaded asynchronously with content. divContent 是一个空的 div,它位于母版页中,并与内容异步加载。

From what I could interpret from your code, you are not actually removing the previous forms.根据我从您的代码中可以解释的内容,您实际上并没有删除以前的 forms。 You are just removing the dialog from them.您只是从他们那里删除对话框。

The method is described as:该方法描述为:

Remove the dialog functionality completely.完全删除对话框功能。 This will return the element back to its pre-init state.这会将元素返回到其预初始化 state。

Meaning its still there.意味着它仍然存在。

Try this (note the $ ('#divForm').remove(); ):试试这个(注意 $ ('#divForm').remove(); ):

 $.post('/Item/Save', postData, function (response, status) {
            $('#divForm').remove();
            $('#divContent').html(response); //response mark up contains a new 'divForm' element
        }, 'html');
    }

With my solution i'm using使用我的解决方案

dialog('destroy') instead dialog('close')对话框(“销毁”)而不是对话框(“关闭”)

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

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