Javascript and Java get simple examples of various form form information

  • 2020-03-30 01:45:40
  • OfStack

As you know, we used multiple input forms when submitting the form. However, not every input form is as simple as document.getelementbyid. There are some combined forms like a checkbox or a radio or a select and how do we use javascript to get and get the submitted arguments from the server? Say more useless, on the code:

Jsp - HTML code:


 <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="hobby" value="1"  />
        basketball :<input type="checkbox" name="hobby" value="2"  />
        Bounce the ball :<input type="checkbox" name="hobby" value="3"  />
        The ball fights :<input type="checkbox" name="hobby" 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:

function check(){

   var radio = document.getElementsByName("xingbie");
   var checkbox = document.getElementsByName("hobby");
   var select = document.getElementById("opt");

   //Get the select tag
   var index = select.selectedIndex;
   var text = select.options[index].text;
   var value = select.options[index].value;

   //Get 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;
   }
   //Get the checkbox label
   for(var i=0;i<hobbys.length;i++){
    if(hobbys[i].checked){
     alert(hobbys[i].value);
    }
    continue;
   }

   //Submit the form form
   document.formkk.submit();

   
  }

Java:

 String[] hobbys = request.getParameterValues("hobby");  //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 articles: