Form element value get way JS and Java mode

Source: Internet
Author: User

    1. We all know that we used a variety of input forms when we submitted the form. However, not every input form is simple and can be obtained in a document.getElementById way. There are some combinations of form similar to checkboxes or radio or select how do we get and get the arguments that are submitted in the server with JavaScript? More useless, the code:
    2. Jsp-html Code:
    3. <form action="input.do" name="FORMKK" >
    4. <table>
    5. <tbody>
    6. <tr>
    7. <td>text:</td>
    8. <td>
    9. <input type="text" name="text" >
    10. </td>
    11. </tr>
    12. <tr>
    13. <td>password:</td>
    14. <td>
    15. <input type="password" name="pass" >
    16. </td>
    17. </tr>
    18. <tr>
    19. <td>radio:</td>
    20. <td>
    21. <input type="Radio" name="Xingbie" value="1" >
    22. Man
    23. <input type="Radio" name="Xingbie" value="2" >
    24. Woman
    25. </td>
    26. </tr>
    27. <tr>
    28. <td>checkbox:</td>
    29. <td>
    30. Football: <input type="checkbox" Name="hobby" value="1"/>
    31. Basketball: <input type="checkbox" Name="hobby" value="2"/>
    32. Bounce: <input type="checkbox" Name="hobby" value="3"/>
    33. Bucket ball: <input type="checkbox" Name="hobby" value="4"/>
    34. </td>
    35. </tr>
    36. <tr>
    37. <td>hidden:</td>
    38. <td>
    39. <input type="hidden" value="123" Name="hidden"/>
    40. </td>
    41. </tr>
    42. <tr>
    43. <td>option:</td>
    44. <td>
    45. <select name="opt" id= "opt" >
    46. <option>1</option>
    47. <option>2</option>
    48. <option>3</option>
    49. <option>4</option>
    50. </select>
    51. </td>
    52. </tbody>
    53. </table>
    54. <input type="button" value="submit" onclick="Javascript:check ()"/>
    55. </form>
    56. Javascript:
    57. function Check () {
    58. var radio = Document.getelementsbyname ("Xingbie");
    59. var checkbox = document.getelementsbyname ("hobby");
    60. var select = document.getElementById ("opt");
    61. //Get Select label
    62. var index = Select.selectedindex;
    63. var text = Select.options[index].text;
    64. var value = Select.options[index].value;
    65. //Get Radio tags
    66. For (var i=0;i<xingbie.length;i++) {
    67. if (Xingbie.item (i). Checked) {
    68. var val = xingbie.item (i). getattribute ("value");
    69. Break ;
    70. }
    71. continue;
    72. }
    73. //Get CheckBox label
    74. For (var i=0;i
    75. if (hobbys[i].checked) {
    76. alert (Hobbys[i].value);
    77. }
    78. continue;
    79. }
    80. //Submit Form form
    81. Document.formkk.submit ();
    82. }
    83. Java:
    84. string[] Hobbys = request.getparametervalues ("hobby"); //checkbox
    85. String Text = Request.getparameter ("text"); //text
    86. String password = request.getparameter ("password"); //password
    87. String Xingbie = Request.getparameter ("Xingbie"); //radio
    88. Request.getparameter ("hidden");
    89. Request.getparameter ("opt"); //select

Form element value get way JS and Java mode

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.