zoukankan      html  css  js  c++  java
  • 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这个数字的值从而出现错误的判断。

  • 相关阅读:
    读书笔记_Effective_C++_条款二:尽量以const, enum, inline替换#define
    读书笔记_代码大全_第14章_组织直线型代码_第15章_使用条件语句
    读书笔记_代码大全_第31章_布局与风格
    读书笔记_代码大全_第16章_控制循环
    读书笔记_Effective_C++_条款五:了解C++默默编写并调用哪些函数
    python学习笔记(三)——字符串
    python学习笔记(六)——函数
    Windows下安装Django
    python小专题——time模块
    python学习笔记(四)——数据字典
  • 原文地址:https://www.cnblogs.com/gssajl/p/2716285.html
Copyright © 2011-2022 走看看