简体   繁体   English

从servlet的html表单中检索select的标题

[英]retrieve caption of select from html form in servlet

I have an html form with a list, like below 我有一个带有列表的html表单,如下所示

<select name="myselect">
   <option value="101">test val 1</option>
   <option value="102">test val 2</option>
   <option value="103">test val 3</option>
</select>

I am able to retrieve the option value in my servlet, using getParameter() method 我可以使用getParameter()方法在我的servlet中检索选项值

But is there any way to get the caption of the selected option (ie. eg: test val 1) 但是有什么方法可以获取所选选项的标题(例如:test val 1)

No. But you can bind the caption to the value in html 否。但是您可以将标题绑定到html中的值

<option value="101:test val 1">test val 1</option>

Now split from : 现在从:

String value = request.getParameter("myselect");
String val1 = value.split(":")[0];
String val2 = value.split(":")[1];

as Roshana mentioned above, it is possible to bind the caption with the value. 如上述Roshana所述,可以将标题与值绑定。 But it is difficult when there is a need to access options using js or ajax. 但是,当需要使用js或ajax访问选项时,这很困难。 So I found a similar solution which is to copy the selected value to an hidden field and access it from servlet. 因此,我找到了一个类似的解决方案,即将选择的值复制到一个隐藏的字段中,然后从servlet中访问它。

<input type="hidden" id="selectCaption">
<select onchange="document.getElementById('selectCaption').value=this.text">

this doesn't change the value and gives no complications, and can simply access the value from servlet. 这不会改变值,也不会带来任何复杂性,并且可以简单地从servlet访问该值。

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

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