[英]jquery dialog load form via ajax, submit form back to modal dialog
我結合了幾個stackoverflow問題來獲取下面的代碼。 這些鏈接通過ajax以模式罰款的方式加載編輯表單,但是在提交表單時,整個頁面都會提交並重新加載。 我只想重新加載模態。
<script charset="utf-8" type="text/javascript">
$(document).ready(function() {
jQuery(".ajax").click( showDialog );
//variable to reference window
$myWindow = jQuery('##myDiv');
//instantiate the dialog
$myWindow.dialog({ height: 350,
width: 400,
modal: true,
position: 'center',
autoOpen:false,
title:'Edit Policy',
overlay: { opacity: 0.5, background: 'black'},
open: function(type,data) { $(this).parent().appendTo("form"); }
});
}
);
//function to show dialog
var showDialog = function() {
$('##myDiv').load(
this.href,
{},
function (responseText, textStatus, XMLHttpRequest) {
//if the contents have been hidden with css, you need this
$myWindow.show();
//open the dialog
$myWindow.dialog("open");
}
);
//prevent the browser to follow the link
return false;
}
//function to close dialog, probably called by a button in the dialog
var closeDialog = function() {
$myWindow.dialog("close");
}
</script>
您有兩種選擇:
提交后,處理更新模型內容。
嘗試通過jQuery load
方法提交表單,如下所示:
$("#myDiv").load("your_submit_page_url_here", $("#yourFormIdHere").serializeArray());
serializeArray
方法會將表單內容轉換為load
方法接受的JSON格式。 整個調用將基本上將提交后的頁面內容加載到myDiv
。 yourFormIdHere
是您賦予<form>
標記的ID
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.