1

.asp ページでチェックボックスを動的に作成する際に問題が発生しています。テーブルのセルの内側で次のコードを使用しています (注 - rsMaint はレコードセットです)。

<%
    if not rsMaint.EOF then     
    rsMaint.moveFirst

    index = 1
%>
    <%
        do while not rsMaint.EOF                
    %>
    <% 
        Response.Write(CreateLabel(rsMaint.fields.getValue("name"),0) )         
        Response.Write("<INPUT type=""checkbox"" id=cb" & index & " value=" & rsMaint.fields.getValue("template_id") & ">")

            rsMaint.moveNext()  
            index = index + 1
            loop
    %>

これにより、チェックボックスが作成され、ソースを表示して、ID の cb1、cb2、cb3 などがあることを確認できます。実行しようとすると、オブジェクトが存在しないというエラーが表示されます。

if cb1.getChecked() = true Then
...
end if 
4

1 に答える 1

3
<%
  rsMaint.moveFirst
  index = 1

  While Not rsMaint.EOF                
    val = rsMaint.fields.getValue("template_id")

    Response.Write(CreateLabel(rsMaint.fields.getValue("name"),0) )         
    Response.Write("<INPUT type=""checkbox"" id="""cb" & index & """" & _
                   " name="""checkbox_" & index & """" & _
                   " value=""" & Server.HTMLEncode(val) & """>")

    rsMaint.moveNext()  
    index = index + 1
  Wend
%>

後で、ユーザーがフォームを投稿したときに、次のことができます。

<%
  If Request("checkbox_1") > "" Then 
    ''# ...
  End If
%>

最初にHTMLエンコードせずにデータ値を出力してはならないことに注意してください。

于 2011-05-26T21:53:11.927 に答える