示例:
apply_type,apply_reason:為數據庫中取出來的值 注意:必須做去空格處理
apply_reason="," & Replace(Trim(dbRs("apply_reason"))," ","") & ","
apply_type="," & Trim(dbRs("apply_type")) & ","
單選框:
<% strSql="SELECT Dict_type,Dict_name FROM dbo.TS_Dictionary WHERE Catalog_id='3056' ORDER BY Dict_type" dbRs.open strSql,dbConn,1,1 IF dbRs.RecordCount>0 THEN dbRs.MoveFirst DO WHILE NOT dbRs.EOF Dict_type=Trim(dbRs("Dict_type")) Dict_name=Trim(dbRs("Dict_name")) %> <input type="radio" id="type_<%=Dict_type%>" name="apply_type" value="<%=Dict_type%>" <%if instr(apply_type,("," & Dict_type &","))>0 then response.write "checked" end if%> /><label for="type_<%=Dict_type%>"><%=Dict_name%></label> <% dbRs.MoveNext LOOP END IF dbRs.close %>
多選框:
<%
strSql= " SELECT Dict_type,Dict_name FROM dbo.TS_Dictionary WHERE Catalog_id='3066' ORDER BY CONVERT(INT,Dict_type) "
dbRs.open strSql,dbConn, 1, 1
IF dbRs.RecordCount> 0 THEN
dbRs.MoveFirst
DO WHILE NOT dbRs.EOF
Dict_type= Trim(dbRs( " Dict_type "))
Dict_name= Trim(dbRs( " Dict_name "))
IF Dict_name= " 其他 " THEN
%>
< input type ="checkbox" id ="reason_other" name ="apply_reason" <%if instr(apply_reason,("," & Dict_type &",")) >0 then response.write "checked" end if%> value=" <%=Dict_type %>" /> < label
for ="reason_other" > <%=Dict_name %> </ label >
< input type ="text" name ="apply_other" style ="border-style: none none solid none;
border-bottom-width: 1px; border-bottom-color: #000000" />
<% ELSE %>
< input type ="checkbox" id ="reason_<%=Dict_type%>" name ="apply_reason" value ="<%=Dict_type%>" <%if instr(apply_reason,("," & Dict_type &",")) >0 then response.write "checked" end if%> /> < label
for ="reason_<%=Dict_type%>" > <%=Dict_name %> </ label >
<% END IF
dbRs.MoveNext
LOOP
END IF
dbRs.close
%>
strSql= " SELECT Dict_type,Dict_name FROM dbo.TS_Dictionary WHERE Catalog_id='3066' ORDER BY CONVERT(INT,Dict_type) "
dbRs.open strSql,dbConn, 1, 1
IF dbRs.RecordCount> 0 THEN
dbRs.MoveFirst
DO WHILE NOT dbRs.EOF
Dict_type= Trim(dbRs( " Dict_type "))
Dict_name= Trim(dbRs( " Dict_name "))
IF Dict_name= " 其他 " THEN
%>
< input type ="checkbox" id ="reason_other" name ="apply_reason" <%if instr(apply_reason,("," & Dict_type &",")) >0 then response.write "checked" end if%> value=" <%=Dict_type %>" /> < label
for ="reason_other" > <%=Dict_name %> </ label >
< input type ="text" name ="apply_other" style ="border-style: none none solid none;
border-bottom-width: 1px; border-bottom-color: #000000" />
<% ELSE %>
< input type ="checkbox" id ="reason_<%=Dict_type%>" name ="apply_reason" value ="<%=Dict_type%>" <%if instr(apply_reason,("," & Dict_type &",")) >0 then response.write "checked" end if%> /> < label
for ="reason_<%=Dict_type%>" > <%=Dict_name %> </ label >
<% END IF
dbRs.MoveNext
LOOP
END IF
dbRs.close
%>
關鍵語句是:<%if instr(apply_reason,("," & Dict_type &","))>0 then response.write "checked" end if%>
將input的值也做"," & Dict_type &","處理,是為了防止 當值為1時, apply_reason中有11或10等包含1這個數字的值從而出現錯誤的判斷。