例19 获取文本框/编辑框/隐藏域的值
填写文本框/编辑框/隐藏域的值:
<form name="form" methord="post" action="dealwith.jsp">
文本框:<input name="textOne" type="text">
编辑框:<textarea name="textTwo" cols="30" row="8"></textarea>
隐藏值:<input name="textThree" type="hidden" value="男">
</form>
获取文本框/编辑框/隐藏域的值:
<%=request.getParameter("textOne")%>
<%=request.getParameter("textTwo")%>
<%=request.getParameter("textThree")%>
例20 获取下拉列表/菜单的值
选择下拉列表/菜单的值
<script language="javascript" type="">
function checkEmpty(form){
for(i=0;i<form.length;i++){
if(form.elements[i].value==""){
alert("表单信息不能为空");
return false;
}
}
}
</script>
<form name="form" method="post" action="dealwith.jsp" onSubmit="return checkEmpty(form)">
选择图书
<select name="select">
<option value="">请选择</option>
<option value="jsp开发手册"> jsp开发手册 </option>
<option value="jsp技术大全"> jsp技术大全 </option>
<option value="jsp宝典"> jsp宝典 </option>
</select>
</form>
获取下拉列表/菜单的值
<%=request.getParameter("select")%>
例21 获取复选框值
复选框:
<script language="javascript" type="">
function checkEmpty(form) {
for (i=0;i<form.length;i++){
if (form.elements[i].value = = ""){
alert("不能为空");
return false;
}
}
}
</script>
<%
String bookType[] = {"科技图书","教育图书","文学图书","医学图书","法律图书"};
int number = bookType.length;
%>
<form name="form" method="post" action="dealwith.jsp" onSubmit="return checkEmpty(form)">
选择书籍:
<%
for (int i=0; i<bookType.length; i++) {
%>
<input type="checkbox" name="name<%=i%>" value="<%=bookType[i] %>" >
<%=bookType[i]%>
<% } %>
<input type="hidden" name="number" value="<%=bookType.length%>" >
<input type="submit" name="Submit" value="提交">
</form>
获取复选框的值
<%
int number=Integer.parseInt(request.getParameter("number"));
%>
您选择为:
<%
for(int i=0;i<number;i++){
String name="name"+i;
if(request.getParameter(name)!=null){
%>
<%=request.getParameter(name)%>
<%}}%>
例22 获取单选按钮的值
单选按钮:
<%
String book[]={"非常好","很好","一般好","很差","非常差"};
int number=book.length;
%>
<form name="form" method="post" action="dealwith.jsp" onSubmit="return checkEmpty(form)">
请选择您本书的满意程度:
<%
for(int i=0;i<book.length;i++){
%>
<input type="radio" name="name" value="<%=book[i]%>">
<%=book[i]%>
<%}%>
<input type="submit" name="Submit" value="提交">
</form>
获取单选按钮的值
您选择为:
<%=request.getParameter("name")%>