js 与 jsp 数据互传

jsp -> js

1.input  和· span

通过定义id 传值:     <input type="text" name="useMoney" id="useMoney" />

                              <span id="confirmspan"></span>

             接收值:      document.getElementById(定义的id);

                                input中 type ="text" 记得document.getElementById(**).value;


2.<% %>

记得<% %> 需要在 <script></script> 上面 (按正常从上到下顺序 执行,js 中执行函数可往上跳)。

3.

初学者,待补充


js -> jsp

1.(转自 博客园 Genter )

-- 以下是JSP中HTML代码,用于将JS中变量存储到隐藏控件中

1
2
3
4
<! -- 在form中设置隐藏控件,用来存储JS中的值 -->
<form name = "frmApp" action = "a.jsp" id= "frmAppId" mothed= "post" />
    <input id= "test" type= "hidden" name = "test" >
</form>

  -- 以下是JSP中JS代码,通过表单将提交本页面(隐藏控件将被提交到服务器)

1
2
3
4
5
6
7
8
9
10
11
<script language= "javascript" >
     function setItemValue(){
         var tmp = "testing" ;
         document.getElementById( "test" ).value = tmp;  // 将JS变量值存储到隐藏控件中
     }
 
     function submit(){
         var frm = document.getElementById( "frmAppId" ); // 获取表单
         frm.submit(); // 对表单进行提交
     }
</script>

  -- 以下是a.jsp中在表单进行提交后,对变量进行接收的代码

1
2
3
<%
     String test = request.getParameter( "test" ); // test为隐藏控件名
%>





猜你喜欢

转载自blog.csdn.net/qq_41902618/article/details/81040375
今日推荐