如何在相同的jsp页面中将javascript代码中的值获取到j

如何在相同的jsp页面中将javascript代码中的值获取到j

本文介绍了如何在相同的jsp页面中将javascript代码中的值获取到jsp scriptlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码(1.jsp)

below is my code (1.jsp)

<html>
<head>
  <script type="text/javascript">

   function changeFunc() {
var selectBox = document.getElementById("selectBox");
var selectedValue = selectBox.options[selectBox.selectedIndex].value;
  document.write("\n value is"+selectedValue);
  }

 </script>
</head>
 <body>
<form method="post" action="SampServlet">
  <select id="selectBox" name="selurl" onchange="changeFunc();">
   <option value="1">Option #1</option>
   <option value="2">Option #2</option>
  </select>
</form>
 </body>
</html>

在这里,我已将此代码插入到一个jsp页面中.并使用相同的jsp从javascript到scriptlet中获取"selectedValue"的值.

Here I have inserted this code into a jsp page.And getting the value of "selectedValue" from javascript to scriptlet with in the same jsp like this.

<% String val=(String)request.getParameter("selurl");
System.out.println("\n selected value is:"+val); %>

我将选择的值作为null输出.而且如果我打印javascript selectedValue参数,它将为我提供正确的输出,即输出为selected选项.但是在scriptlet中却为null.哪里出错了,我包括了所有标头和指令.请帮帮我.

I am getting selected value as null as output. And if I print javascript selectedValue parameter it is giving me correct output i.e.,output as the option selected.But in scriptlet am getting null.Where is the error.I included all headers and directives.Please help me.

推荐答案

使用提交按钮在同一页面上获取所选值",然后不需要任何功能,不需要提交.

Use submit Button to get Your Selected value at same page andno need any function,no need onsubmit.

例如:

<form method="post" action="">
 <select id="selectBox" name="selurl">
<option value="1">Option #1</option>
<option value="2">Option #2</option>
</select>
<input type="submit" value="Submit" name="sub">
                                                //use scriplet tag
<% String r=request.getParameter("sub");
if(r.equals("Submit"){
String s=request.getParameter("selurl");
System.out.println("selected value is "+s);
}%>
</form>

这篇关于如何在相同的jsp页面中将javascript代码中的值获取到jsp scriptlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 18:19