javascript 我如何在提交表单时关闭我的 jquery ui 对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3668245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i close my jquery ui dialog when i submit a form?
提问by leora
i load a form into a jquery ui dialog. i have a submit button (inside my form - NOT the actual dialog buttons) that calls a controller action but i can't figure out how to close the dialog after the submit is called as i dont have any event handler that i am attaching.
我将表单加载到 jquery ui 对话框中。我有一个提交按钮(在我的表单中 - 不是实际的对话框按钮),它调用控制器操作,但我无法弄清楚如何在调用提交后关闭对话框,因为我没有附加任何事件处理程序。
is there anyway of doing this besides changing the submit to input type=button?
除了将提交更改为输入类型=按钮之外,还有其他方法吗?
i know in jquery i can capture the submit
我知道在 jquery 中我可以捕获提交
$('#positionForm').submit(function () {
// do stuff
return true;
});
but this seems to fire before submitting so i dont want to close the dialog yet.
但这似乎在提交之前触发,所以我不想关闭对话框。
is there anything wrong with the below code:
下面的代码有什么问题吗:
$('#positionForm').live('submit', function () {
$.post('/MyController/Action', $("#positionForm").serialize(), function (data) {
alert(data);
}, "html");
closeModalPopup();
return false ;
});
回答by Nick Craver
For updated question:You can call the close code in the successcallback, like this:
对于更新的问题:您可以在success回调中调用关闭代码,如下所示:
$('#positionForm').live('submit', function () {
$.post('/MyController/Action', $(this).serialize(), function(data) {
$('#positionForm').closest(".ui-dialog-content").dialog("close");
}, "html");
return false;
});
Original Answer:You can attach a form submithandler, for example:
原始答案:您可以附加一个表单submit处理程序,例如:
$("#myform").submit(function() {
$(this).closest(".ui-dialog-content").dialog("close");
});
回答by oaceballos
You can also do it using $("#name_of_the_dialog").dialog("close");
你也可以使用$("#name_of_the_dialog").dialog("close");
It's more natural than using $closest
这比使用更自然 $closest

