表单提交,回显数据,后退处理

本文介绍了几种表单提交的方法,包括使用JavaScript进行表单回显、表单提交至新页面并保留原始页面状态等技巧。此外,还展示了如何在用户操作后返回上一页并清空表单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、提交到本页,可以使用<script>history.go(-1);</script>,回显数据

<html>
<%
    
String ss = request.getParameter("ss")==null?"":request.getParameter("ss");
    
if( ss.equals("add") ){
    
String n1 = request.getParameter("n1");
    
String n2 = request.getParameter("n2");
    out.print(n1);
%>
    
<script type="text/javascript">
        alert(
"window.history.go(-1)");
        window.history.go(
-1);
</script>
<%

    }
%>
<head>
<script>
    
function s(){
        form1.ss.value
="add";
        alert(
"form submit");
        form1.submit();
        
//alert(document.all.form1.ss.value);
    }

    
</script>
</head>

<body>
    
<form action="" method="post" name="form1" id="form1" >
    
<table width="500" border="1" align="center" cellpadding="0" cellspacing="0">
  
<tr>
    
<td>n1
      
<input type="text" name="n1" id="n1" /></td>
  
</tr>
  
<tr>
    
<td>n2
      
<input type="text" name="n2" id="n2" /></td>
  
</tr>
  
<tr>
    
<td>s      
      
<input type="button" name="button" id="button" value="Submit" onClick="s()">
      
<input type="hidden" name="ss" id="ss" value="op">
      
<input type="hidden" name="ss1" id="ss1"></td>
  
</tr>
</table>
    
</form>
</body>
</html>

 

2、<form ...target="_blank">...</form>,提交表单到新的页面 处理,表单页面不变。但是关闭 处理 页面需要用户确认

3、提交表单到 新的页面,新页面 使用<script>history.go(-1);</script>,回显数据,同时修改 按钮。

1.jsp

<html>
<head>
<script>
    
function s(){
        form1.ss.value
="add";
        alert(
"form submit");
        document.form1.action
="2.jsp";
        document.form1.submit();
    }

    
function c(){
        form1.reset();
    }

    
function checkBack(){
        
if (form1.ss.value == "add" ){
            form1.ok.value 
= "修改";
        }

        alert(form1.ss.value);
    }

    
</script>
</head>

<body onLoad="checkBack()">
    
<form action="" method="post" name="form1" id="form1" >
    
<table width="500" border="1" align="center" cellpadding="0" cellspacing="0">
  
<tr>
    
<td>n1
      
<input type="text" name="n1" id="n1" />
      
<select name="select" id="select">
        
<option value="1">1</option>
        
<option value="2">2</option>
        
<option value="3">3</option>
      
</select>
      
</td>
  
</tr>
  
<tr>
    
<td>n2
      
<input type="text" name="n2" id="n2" /></td>
  
</tr>
  
<tr>
    
<td>s      
      
<input type="button" name="ok" id="button" value="提交" onClick="s()">
      
<input type="button" name="nok" value="重置" onClick="c()">

      
<input type="hidden" name="ss" id="ss" value="op">
      
<input type="hidden" name="ss1" id="ss1"></td>
  
</tr>
</table>
    
</form>
</body>
</html>

2.jsp

<%
    
String ss = request.getParameter("ss")==null?"":request.getParameter("ss");
    
if( ss.equals("add") ){
    
String n1 = request.getParameter("n1");
    
String n2 = request.getParameter("n2");
//    out.print(n1);
%>
    
<script type="text/javascript">
        alert(
"window.history.go(-1)");
        
//opener.form1.action="";
        //opener.form1.ok.value="打印";
        //opener.form1.nok.value="修改";
        window.history.go(-1);

        
//window.close();
</script>
<%

    }
%>

 4、后退清空表单

<META NAME="save" CONTENT="history">

       保存

Persistence可以不用通过服务器保持页面上的信息,从而加快了浏览速度。
是作为一个behavior执行的,新的persistence行为包括:

saveFavorite - 在网页被添加到收藏夹时保持页面的状态和信息。

saveHistory - 在当前的会话内存中保持页面的状态和信息。

saveSnapshot - 当用户把网页存到本地硬盘时,直接把页面的状态和信息保留在网页上。

userData - 在XML store中保持页面的信息和状态。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值