在ajax流行之前如何實現無刷新提交表單呢?其實用隱藏的iframe完全可以實現該功能,看一個測試的小例子。
ryAdd.jsp
<style type="javascript/text">
// 全局方法
function reset(){
$("txtID").val("");
$("txtName").val("");
}
</script>
<form action="xy/ryAdd.action" target="frame">
<table>
<tr>
<td>編號</td>
<td><input type="text" name="txtID" id="txtID"></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="txtName" id="txtName"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
<tr>
<td><span id="hint"></span></td>
</tr>
</table>
</form>
<iframe name="frame" style="display:none"></iframe>
Action
public class ryAction
{
private String msg;
public String ryAdd()
{
try
{
...........
msg = "添加成功";
}
cathc(Exception ex)
{
msg = ex.getMessage();
}
return "result";
}
..........省略getter,setter方法..............
}
strust.xml
<action name="ryAdd" class="cn.xy.ryAction" method="ryAdd">
<result name="result">result.jsp</result>
</action>
result.jsp
<head>
<script type="text/javascript">
window.onload = function(){
// 本頁面獲得的提示信息
if(document.getElementById('subhint'))
{
var hint = document.getElementById('subhint').innerHTML;
// 找到父頁面
if(window.parent){
if(window.parent.reset){
window.parent.reset();
}
if(window.parent.document.getElementById('hint')){
window.parent.document.getElementById('hint').innerHTML = hint;
}
}
}
};
</script>
</head>
<body>
<span id="subhint">${requestScope.msg}</span>
</body>