你可以使用AJAX來從第二JSP在HTML,然後附加到DOM或一個元素的innerHTML。
主要Page.jsp與確認碼
<span id=confirmSpan></span>
<script language="Javascript">
function xmlhttpPost(strURL, queryStr) {
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari, opera etc
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}else{
alert("no ajax")
return
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
updatepageConfirm(self.xmlHttpReq.responseText);
}
}
self.xmlHttpReq.send(queryStr);
}
function updatepageConfirm(str){
document.getElementById("confirmSpan").innerHTML = str;
}
function logout1(){
xmlhttpPost("confirm.html");//http://sel2in.com/pages/prog/html/ajax/confirm.html", "")
}
</script>
</head>
<body>
<form id="search" action="/search" method="get">
<input type=button onclick=logout1() value=logout>
</form >
樣品頁面 - >爲DIV任何有效的HTML可能是 confirm.jsp 做動作......你確定嗎?跨度
// TODO形式在這裏 HTML /彈出/腳本/ CSS - >就像一個iframe
看起來要顯示一個確認 - 一些用戶界面問你是否真的要退出?最好的方法是創建一個id爲「logoutConfirmSpan」的空跨度,然後點擊註銷,做ajax(異步模式 - > false,所以它發生內聯)獲取html標記並將其設置爲innerHTML的跨度
該html應該是有效的,這樣它會使用戶界面顯示並且形式才能真正註銷。
見Replace inner HTML of a div with Ajax response告訴你如何使用Ajax與jQuery
簡單的例子:
HTML snippets
<span id = 'logoutConfirmSpan'> </span>
<script>
// call ajax, with the response call setHtmlForConfirm
function setHtmlForConfirm(req)
{
document.getElementById('logoutConfirmSpan').innerHTML = req.responseText;
}
//req.responseText should have the html to show the confirm UI and form to submit yes/ no
</script>
那麼,你有沒有遇到任何錯誤? – 2013-04-23 07:18:50
沒有。我沒有得到任何錯誤。頁面沒有得到加載 – mukund 2013-04-23 07:19:47
嘗試檢查在JavaScript控制檯。 – 2013-04-23 07:20:15