简体   繁体   中英

how do i get the value of the text box that is related to the specific option value when selected

how do i get the value of the text box that is related to the specific option value when selected in servlet/jsp as i want to put it in the database.for eg, when i select the option red then it's related text box(which will have a default value) will be activated or appeared. I know to get the value of the selected option but i don't know to get the value of it's related option. Is there any other way to do it ..??Is there any one can help me please..!!! ??

<html> 
<head>  
<script type="text/javascript">
function CheckColors(val)
{  
var element=document.getElementById('Color');
var element1=document.getElementById('Dolor');
var element2=document.getElementById('Polor');

element.style.display='none';
element1.style.display='none';
element2.style.display='none';

if(val=='others')
{
element.style.display='block';  

}

else if(val=='red')
{

 element1.style.display='block';
}
else if(val=='blue')
{
 element2.style.display='block'; 
}


}

</script> 
</head>
<body>
<select name="color" onchange='CheckColors(this.value);'> 
<option>pick a color</option>  
 <option value="red">RED</option>
 <option value="blue">BLUE</option>
 <option value="others">others</option>
 </select>
 other:<input type="text" placeholder="other" name="Color" id="Color"         style='display:none;'/>
 red:<input type="text" placeholder="red" name="Dolor" id="Dolor" style='display:none;'/>
 blue:<input type="text" placeholder="blue" name="Polor" id="Polor" style='display:none;'/>
</body>
</html>

I would recommend you reading about Java JSP/Servlets. Follow a simple tutorial, google it!

To communicate with a web page and your code you will need to handle it with a submit form. There are different ways to achieve this, but simple is to use a form element, and an action and method. Either you add a submit call in your javascript CheckColors or apply a submit button when color has been chosen.

HTML request page --> Jsp or Servlet --> HTML response page

change:

<html> 
<head>  
...
<body>
<form name='theform' action='/javacode.jsp' method='post'>
 <select name="color" onchange='CheckColors(this.value);'> 
 ...
 <input type="submit" value="submit" name="submit">
 </form>
</body>
</html>

Now if you use a jsp or servlet, doesnt matter. This is simple demo.

In your javacode then, you do read the request with getParameter("color")

Read as I said a JSP / Servlet tutorial.

A tutorial with similar flow, using color as an example:

http://met.guc.edu.eg/OnlineTutorials/JSP%20-%20Servlets/A%20servlet%20example.aspx

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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