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

  • 相关阅读:
    练习
    js简介、基本语法、类型转换、变量、运算符、分支语句、循环语句、函数、函数调用
    样式表、格式布局、流式布局
    表单
    图片热点、网页划区
    JS模拟窗口
    JavaScript 常用功能实现一览(一)
    分页打印控制 摘自于网络:http://www.cnblogs.com/joinger/articles/1807517.html
    我眼中的C#3.0 摘自于网络:http://www.cnblogs.com/joinger/articles/1297237.html
    .net ToString()用法详解与格式说明
  • 原文地址:https://www.cnblogs.com/gssajl/p/2716285.html
Copyright © 2020-2023  润新知