2006年12月28日
復(fù)選框的name值取同名"shrdCategory",通過(guò)String[] shrdCategory=
request.getParameterValues("shrdCategory")取得同名的復(fù)選框的各個(gè)value值
,然后數(shù)組遍歷,取出各個(gè)shrdCategory[i]即可;
代碼如下所示:
環(huán)境保護(hù)<input type="checkbox" name="shrdCategory" value="環(huán)境保護(hù)">
國(guó)民經(jīng)濟(jì)<input type="checkbox" name="shrdCategory" value="國(guó)民經(jīng)濟(jì)">
String[] shrdCategory=request.getParameterValues("shrdCategory");
for (int i=0;i<shrdCategory.Length;i++){
System.out.println(shrdCategory[i]);
}
但是要注意,上面這段代碼jsp運(yùn)行時(shí)報(bào)505錯(cuò)誤!!為什么呢?
因?yàn)閯傔M(jìn)入頁(yè)面時(shí),沒有復(fù)選框無(wú)值,shrdCategory為null,這時(shí)不能使用
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!