ASP 中如何根据数据库中取出的值来判定 checkbox或radio 的状态是否为选中

示例:

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>&nbsp;&nbsp;&nbsp;
                    <%
                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- 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <%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这个数字的值从而出现错误的判断。

原文地址:https://www.cnblogs.com/gssajl/p/2716285.html