A simple example of Javascript and Java to obtain various form information

Source: Internet
Author: User

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

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.