A simple example of Javascript and Java to obtain various form information
This article mainly introduces simple examples for Javascript and Java to obtain various form information. If you need some help, please refer to them.
We all know that multiple input forms are used when submitting the form. However, it is easy to use Document. getElementById to obtain each input form. Some combined forms are similar to checkbox, radio, or select. How can we use javascript to obtain and submit parameters on the server? Useless words and code:
Jsp-html code:
The Code is as follows:
<Form action = "input. do" name = "formkk">
<Table>
<Tbody>
<Tr>
<Td> text: </td>
<Td>
<Input type = "text" name = "text">
</Td>
</Tr>
<Tr>
<Td> password: </td>
<Td>
<Input type = "password" name = "pass">
</Td>
</Tr>
<Tr>
<Td> radio: </td>
<Td>
<Input type = "radio" name = "xingbie" value = "1">
Male
<Input type = "radio" name = "xingbie" value = "2">
Female
</Td>
</Tr>
<Tr>
<Td> checkbox: </td>
<Td>
Football: <input type = "checkbox" name = "holobby" value = "1"/>
Basketball: <input type = "checkbox" name = "holobby" value = "2"/>
Ball shot: <input type = "checkbox" name = "holobby" value = "3"/>
Ball fighting: <input type = "checkbox" name = "holobby" value = "4"/>
</Td>
</Tr>
<Tr>
<Td> hidden: </td>
<Td>
<Input type = "hidden" value = "123" name = "hidden"/>
</Td>
</Tr>
<Tr>
<Td> option: </td>
<Td>
<Select name = "opt" id = "opt">
<Option> 1 </option>
<Option> 2 </option>
<Option> 3 </option>
<Option> 4 </option>
</Select>
</Td>
</Tbody>
</Table>
<Input type = "button" value = "Submit" onclick = "javascript: check ()"/>
</Form>
Javascript:
The Code is as follows:
Function check (){
Var radio = document. getElementsByName ("xingbie ");
Var checkbox = document. getElementsByName ("holobby ");
Var select = document. getElementById ("opt ");
// Obtain the select tag
Var index = select. selectedIndex;
Var text = select. options [index]. text;
Var value = select. options [index]. value;
// Obtain the radio tag
For (var I = 0; I <xingbie. length; I ++ ){
If (xingbie. item (I). checked ){
Var val = xingbie. item (I). getAttribute ("value ");
Break;
}
Continue;
}
// Obtain the checkbox tag
For (var I = 0; I
If (hobbys [I]. checked ){
Alert (hobbys [I]. value );
}
Continue;
}
// Submit the form
Document. formkk. submit ();
}
Java:
The Code is as follows:
String [] hobbys = request. getParameterValues ("holobby"); // checkbox
String text = request. getParameter ("text"); // text
String password = request. getParameter ("password"); // password
String xingbie = request. getParameter ("xingbie"); // radio
Request. getParameter ("hidden ");
Request. getParameter ("opt"); // select