Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 读取选择框的选定值并将其传递给jsp_Javascript_Jsp_Drop Down Menu_Selectedvalue - Fatal编程技术网

Javascript 读取选择框的选定值并将其传递给jsp

Javascript 读取选择框的选定值并将其传递给jsp,javascript,jsp,drop-down-menu,selectedvalue,Javascript,Jsp,Drop Down Menu,Selectedvalue,我对JSP、HTML等非常陌生。。。我有一个问题: 我有一个JSP,我正试图从HTML选择框中读取选定值,例如,使用JavaScript: <form name="ListForm" action=""> <select name="country" size="6"> <% String[] testArray = {"Germany", "Russia", "China", "Iran", "USA", "Israel"}; for (int

我对JSP、HTML等非常陌生。。。我有一个问题:

我有一个JSP,我正试图从HTML选择框中读取选定值,例如,使用JavaScript:

<form name="ListForm" action=""> 
<select name="country" size="6">
<%
    String[] testArray = {"Germany", "Russia", "China", "Iran", "USA", "Israel"};
    for (int i = 0; i < testArray.length; i++) {
%>
        <option value=<%=testArray[i]%>>
        <%= testArray[i] %> 
        </option>
<%
    }
%>
</select> 
</form>
<script type="text/javascript">
    function getSelectedValue() {
        var e = document.getElementById("country");
        return e.options[e.selectedIndex].text;
    }
</script>

这是JavaScript:

<form name="ListForm" action=""> 
<select name="country" size="6">
<%
    String[] testArray = {"Germany", "Russia", "China", "Iran", "USA", "Israel"};
    for (int i = 0; i < testArray.length; i++) {
%>
        <option value=<%=testArray[i]%>>
        <%= testArray[i] %> 
        </option>
<%
    }
%>
</select> 
</form>
<script type="text/javascript">
    function getSelectedValue() {
        var e = document.getElementById("country");
        return e.options[e.selectedIndex].text;
    }
</script>

函数getSelectedValue(){
var e=document.getElementById(“国家”);
返回e.options[e.selectedIndex].text;
}
现在我想将此字符串传递给另一个JSP:

<% 
    String testVar = request.getParameter("country");
    session.setAttribute("varName", testVar); 
%>


但这不起作用。你知道为什么吗?

一个可能的问题是,你的选择菜单的名称是
国家,而不是id
国家。因此,您不会使用
document.getElementById(“国家”)

您可以通过向
标记添加id来解决此问题:

<select id="country" name="country" size="6">
<form name="ListForm" action="[server url]"> 

请从开发人员的角度而不是最终用户的角度详细说明“不起作用”。代码似乎也不完整。你没有展示你是如何提交表格的。所以我认为问题在于你根本没有提交表格。此外,JavaScript代码不仅是不完整的(
id
name
不一样),而且看起来非常无用,我想知道它为什么会存在。只需删除JS代码,在
中添加一个
,然后按它。或者这不是您要问的吗?当我尝试在下一个jsp中读取保存的参数时,我得到的是“null”而不是所选的值。我不知道我是否提交了表单,但我进入下一个jsp时使用了:

我应该插入一种提交语句吗?谢谢你的帮助!那是真的,谢谢你。但这并没有解决问题。不过,我得到的是“null”而不是所选的值。