如何使选定的下拉选项变粘?

时间:2016-02-17 18:03:26

标签: html asp-classic

我遇到了在经典asp的下拉菜单中粘贴所选选项的问题。如果我保持选择"如下所示,选项值在下拉栏中保持不断变化,直至下拉记录的末尾。换句话说,从选项中选择的内容并不重要,它最终会选择下拉列表中的最后一个选项。但它显示正确的结果。即它正确选择并且工作正常但不会粘在所选的选项上。你认为我保持选择"在错误的地方?

Select: <select class=input>
    <OPTION value=0>-- SELECT --</OPTION>
    <% dim rs
        SQL = "SELECT DISTINCT(brand) as brand FROM tbl"
            SET rs=objConn.Execute(SQL)  

            IF NOT (rs.BOF and rs.EOF) THEN 
                WHILE NOT rs.EOF
                    Response.Write "<option selected value=""" & replace(rs("brand")," & ","@") & """>" & rs("brand") & "</option>"
                    rs.MoveNext
                WEND 
            END IF
                rs.close
                SET rs = nothing
        %>
    </select>

3 个答案:

答案 0 :(得分:2)

这种情况正在发生,因为在你的循环中你将每个选项设置为&#34;选择&#34;浏览器将默认为列表中具有该名称的最后一个选项 您需要决定选择哪一个,并且只包括&#34;选择&#34;在那个选项中。如果你想要第一个(你在循环外创建的那个),就不要使用&#34;选择&#34;一点都不。

例如,如果您想要品牌&#34; Acme&#34;要选择,请执行以下操作:

  dim strQueryBrand
  If Request.QueryString("brandID") <> "" Then
     'add some validation here to avoid cross-site scripting issues'
     strQueryBrand = Request.QueryString("brandID")
  End
  dim isSelected = ""
   IF NOT (rs.BOF and rs.EOF) THEN 
        isSelected = ""
        If LCase(strQueryBrand) = LCase(strBrand) then
            isSelected = "selected "
        End If
        WHILE NOT rs.EOF
            Response.Write "<option " & isSelected & "value=""" & replace(rs("brand")," & ","@") & """>" & rs("brand") & "</option>"
            rs.MoveNext
        WEND 
    END IF

答案 1 :(得分:1)

自从我查看ASP以来已经很长时间了 - 但乍一看,您似乎正在设置每个选项。

以下并不真正有意义,因为您告诉每个选项它是选定的选项。最后一个选项将是最后一个选项,因为它是最近被选中的选项。

<select class=input>
    <option selected value="1">1</option>
    <option selected value="2">2</option>
    <option selected value="3">3</option>
    <option selected value="4">4</option>
</select>

您完成的选择应该只有一个选项使用&#34;选择&#34;属性

<select class=input>
    <option value="1">1</option>
    <option selected value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

答案 2 :(得分:1)

您可以选择多种输出方式来编写输出。 我猜你正在使用一个字符串,但brandID给了我印象,你的默认值是数字,我在想整数。 无论如何,请按照以下步骤选择“已选择”。

Select: <select class=input onChange="if(options[selectedIndex].value != 0){location='test.asp?list=bybrand&brandID=' + options[selectedIndex].value + '', exit=false;}">
    <% dim rs,strBrand,strQueryBrand
    If Request.QueryString("brandID") <> "" Then
    strQueryBrand = Request.QueryString("brandID")
      If InStr(strQueryBrand,"@") >= 1 Then
         strQueryBrand = Replace(strQueryBrand,"@"," & ")
      End If
    Else
    strQueryBrand = 0
    End If
    '# clean strQueryBrand to prevent SQL injection
        SQL = "SELECT DISTINCT(brand) as brand FROM tbl"
            SET rs=objConn.Execute(SQL)  
            IF NOT (rs.BOF and rs.EOF) THEN 
        If strQueryBrand = 0 Then
        Response.Write "<OPTION value=0 selected>-- SELECT --</OPTION>"
        End If
                WHILE NOT rs.EOF
                strBrand = rs("brand") 
                If LCase(strQueryBrand) = LCase(strBrand) Then
                    Response.Write "<option selected value=""" & replace(rs("brand")," & ","@") & """>" & rs("brand") & "</option>"
                Else            
                    Response.Write "<option value=""" & replace(rs("brand")," & ","@") & """>" & rs("brand") & "</option>"
                End If
                rs.MoveNext
                WEND 
            END IF
                rs.close
                SET rs = nothing
        %>
    </select>

如果是您正在使用的数值,请替换以下内容。

If LCase(strQueryBrand) = LCase(strBrand) Then

If (cInt(strQueryBrand) - cInt(strBrand)) = 0 Then

使用Jen R答案的 PART 添加了另一种方法。     如果您有动态更改选择内容,您必须在循环中进行选择,或者它只是STATIC一个答案,那么您不必担心选中,因为它每次都是相同的。

Select: <select class=input onChange="if(options[selectedIndex].value != 0){location='test.asp?list=bybrand&brandID=' + options[selectedIndex].value + '', exit=false;}">
<% dim rs,strBrand,strQueryBrand,isSelected
    If Request.QueryString("brandID") <> "" Then
    strQueryBrand = Request.QueryString("brandID") 
      If InStr(strQueryBrand,"@") >= 1 Then
         strQueryBrand = Replace(strQueryBrand,"@"," & ")
      End If
    Else
    strQueryBrand = 0
    End If
'# clean strQueryBrand to prevent injection
    SQL = "SELECT DISTINCT(brand) as brand FROM tbl"
        SET rs=objConn.Execute(SQL)  

        IF NOT (rs.BOF and rs.EOF) THEN 
        If strQueryBrand = 0 Then
        Response.Write "<OPTION value=0 selected>-- SELECT --</OPTION>"
        End If
            WHILE NOT rs.EOF
            strBrand = rs("brand") 
            If LCase(strQueryBrand) = LCase(strBrand) Then
                isSelected = "selected "
            End If           
                Response.Write "<option "&isSelected&" value=""" & replace(rs("brand")," & ","@") & """>" & rs("brand") & "</option>"

            rs.MoveNext
            WEND 
        END IF
            rs.close
            SET rs = nothing
    %>
</select>