日期:2014-05-16  浏览次数:20354 次

jsp和js两种方式取到表单数据
<form action="" method="post" onsubmit="return check()" name="selectform">
  <input type="radio" name="gender" value="man" checked/>男
  <input type="radio" name="gender" value="woman" />女
  <input type="checkbox" name="animal" value="cat" checked/> cat
  <input type="checkbox" name="animal" value="dog"/> dog
  <input type="checkbox" name="animal" value="elephant"/> elephant
  <input type="checkbox" name="animal" value="lion"/>lion
  <select name="select" id="selected">
    <option value="1">选项一
    <option value="2">选项二
    <option value="3">选项三
    <option value="4" selected>选项四
 <input type="hidden" name="hidden" id="hidden" value="100" />
  </select>
  <input type="submit" value="submit"/>
</form>

Jsp方式取得:

<%
 String gender = request.getParameter("gender");
 out.print(gender);
 String[] animal = request.getParameterValues("animal");
 for(int i=0;i<animal.length;i++){
  out.print(animal[i]);
 } 
 String select = request.getParameter("select");
 out.println(select);
 String hidden = request.getParameter("hidden");
 out.println(hidden);
%>

JavaScript方式取得:
<script type="text/javascript">
function check(){
 var gender = document.getElementsByName("gender");
// var gender = document.selectform.gender;一样能拿到gender这个对象,其实是个数组!
 for(i=0;i<gender.length;i++){
  if(gender[i].checked)
  alert(gender[i].value);
 }
 
 var animal = document.getElementsByName("animal");
// var gender = document.selectform.animal;
 for(i=0;i<animal.length;i++){
  if(animal[i].checked)
  alert(animal[i].value);
 }
 
 var select = document.getElementById("selected");//拿到一个对象
// var select = document.selectform.select;
 for(i=0;i<select.options.length;i++){
  if(select.options[i].selected)
  alert(select.options[i].value);
 }
 var selectedOption = document.selectform.select.options[document.selectform.select.selectedIndex];
 var selectedOptionValue = selectedCategory.value;


 var hidden = document.getElementById("hidden");
//  var hidden = document.selectform.hidden;
 alert(hidden.value);
 
 
 return true;
}