2006年12月28日
復選框的name值取同名"shrdCategory",通過String[] shrdCategory=
request.getParameterValues("shrdCategory")取得同名的復選框的各個value值
,然后數組遍歷,取出各個shrdCategory[i]即可;
代碼如下所示:
環境保護<input type="checkbox" name="shrdCategory" value="環境保護">
國民經濟<input type="checkbox" name="shrdCategory" value="國民經濟">
String[] shrdCategory=request.getParameterValues("shrdCategory");
for (int i=0;i<shrdCategory.Length;i++){
System.out.println(shrdCategory[i]);
}
但是要注意,上面這段代碼jsp運行時報505錯誤!!為什么呢?
因為剛進入頁面時,沒有復選框無值,shrdCategory為null,這時不能使用
shrdCategory[i];
解決:
加上非null判斷:
String[] shrdCategory=request.getParameterValues("shrdCategory");
if (shrdCategory!=null){
for (int i=0;i<shrdCategory.Length;i++){
? System.out.println(shrdCategory[i]);
? }
}
ok!