简体   繁体   English

在jsp页面中显示选定的文本

[英]Display selected text in a jsp page

Hi I am a newbie in JSP and was trying to show the selected value in a header as a dynamic text. 嗨,我是JSP的新手,试图将标头中的选定值显示为动态文本。 But every time I select it shows the option value not the text. 但是,每次选择它时,它都会显示选项值而不是文本。 below is what I have tried. 下面是我尝试过的。

Html drop down HTML下拉

<select id="country" name="country_code">
       <option value="1">Puerto Rico</option>
       <option value="63">Philippines</option>
       <option value="others">Others</option>
</select>

JSP tag JSP标签

    <%
        String countrycd = request.getParameter("country_code");
        String countryTxt = request.getParameter("country_code");
    %>

This is my script 这是我的剧本

    <script>
        document.getElementById("country").value = '<% out.print(countrycd); %>';
        var e= document.getElementById("country");
        var str = e.options[e.selectedIndex].text;
        alert(str);
    </script>

This is my html code where I need to show the selected text instead of value for example if user selects Philippines it should show as Philippines not the code 63 这是我的html代码,我需要在其中显示所选文本而不是值,例如,如果用户选择菲律宾,则应该显示为菲律宾而不是代码63

<h2>By Country: <% out.print(countrycd);</h2>

Any help would be really appriciated! 任何帮助将是真正的应用!

Thanks. 谢谢。

EDIT: 编辑:

Current Output 电流输出

From drop down philipines is selected in header it shows: By Country: 63 从标题的下拉菲律宾中选择,它显示:按国家:63

Expected output: 预期产量:

From drop down if philipines is selected it should show 如果选择了菲律宾,则从下拉列表中应显示

By Country: Philipines not 63. 按国家:菲律宾不是63。

You're showing the value in the header. 您正在标题中显示值。 You could accomplish it by doing: 您可以这样做:

<h2>By Country: <span id="countryName"> </span> </h2>

And then your script should be: 然后您的script应该是:

<script>
  window.onload = function(){        
    document.getElementById("country").value = '<% out.print(countrycd); %>';
    var e= document.getElementById("country");
    var str = e.options[e.selectedIndex].text;

    document.getElementById("countryName").innerHTML = str;  //This line changed
  };
</script>

Instead of getting the country name with JSP, we do it with javascript (writing it inside the countryName span) 而不是使用JSP获取国家名称,我们使用javascript(将其写在countryName范围内)来完成

Cheers, from La Paz, Bolivia 来自玻利维亚拉巴斯的欢呼声

The behavior of the select component in HTML is to send the value of the selected option when the form is posted rather than the display text. HTML中的select组件的行为是在发布表单时发送选定选项的值,而不是显示文本。 You can achieve your objective in one of 2 ways: 您可以通过以下两种方法之一实现目标:

Change the select like this: 像这样更改选择:

   <select id="country" name="country_code">
       <option value="Puerto Rico">Puerto Rico</option>
       <option value="Philippines">Philippines</option>
       <option value="Others">Others</option>
   </select>

Or if the value represents an id of the country in a database you can look it up when the form is posted and get the name of the country that way. 或者,如果该值表示数据库中国家/地区的ID,则可以在发布表单时进行查询,并以这种方式获取国家/地区的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM