简体   繁体   English

如何使选定的下拉选项置为粘性?

[英]How to make a selected dropdown option sticky?

I am having problem with sticking the selected option in the drop-down menu in classic asp. 我在将经典ASP中的下拉菜单中的选定选项粘贴时遇到问题。 If I keep "selected" as shown below, the options values keep on changing in the drop-down bar up to the end of the record in drop-down. 如果我如下所示保持“选择”状态,则选项值将在下拉栏中不断变化,直到下拉列表中的记录结束为止。 In other words, it doesn't matter what you select from the option, it will eventually select the last option in the drop-down list. 换句话说,从选项中选择什么都没有关系,它将最终选择下拉列表中的最后一个选项。 however it display the correct result. 但是它显示正确的结果。 ie it is selecting properly and working fine but doesn't stick on the selected option. 即,它选择正确且工作正常,但不停留在所选选项上。 Do you think I kept "selected" in wrong spot? 您是否认为我在错误的位置保持“选择”状态?

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>

That is happening because in your loop you are setting every option to "selected" and the browser will default to the last option in the list that has that designation. 发生这种情况是因为在循环中,您将每个选项都设置为“选定”,并且浏览器将默认为具有该名称的列表中的最后一个选项。
You need to decide which one you want selected and only include "selected" in that option. 您需要决定要选择哪一个,并且在该选项中仅包括“ selected”。 If you want the first one (the one you created outside the loop), just don't use "selected" at all. 如果您想要第一个(您在循环之外创建的),那就根本不要使用“ selected”。

For example, if you want the brand "Acme" to be selected, do this: 例如,如果要选择品牌“ Acme”,请执行以下操作:

  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

It's been a long time since I've looked at ASP - but at first glance it appears that you are setting every option to be selected. 自从我学习ASP以来已经有很长时间了-但是乍一看似乎您正在设置要选择的每个选项。

The following doesn't really make sense because you are telling each option that it is the selected one. 以下内容实际上没有意义,因为您要告诉每个选项它都是所选的。 Ultimately the last option will be the selected one, because it was the one most recently told to be selected. 最终,最后一个选项将是选定的选项,因为它是最近被告知要选择的一个选项。

<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>

Your completed select should only have one option using the "selected" attribute 使用“ selected”属性,完成的选择应该只有一个选项

<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>

You have many options to write your output. 您有很多选择可以编写输出。 I'm guessing you're using a string but brandID gives me the impression and your default value being numeric I'm thinking integer. 我猜您正在使用字符串,但brandID给我的印象是,而您的默认值是数字,我认为是整数。 In any case, follow the following to select your "Selected". 无论如何,请按照以下步骤选择“已选择”。

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 it is a numeric value you are working with replace the following . 如果它是数字值,则使用以下替换。

If LCase(strQueryBrand) = LCase(strBrand) Then

with

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

Added another method using PART of Jen R answer. 添加了使用Jen R答案的PART的另一种方法。 If you have dynamic changing selection content you must make the selection within the loop or it's simply STATIC one answer then you wouldn't need to worry about the Selected because it would be the same each and every time. 如果您具有动态变化的选择内容,则必须在循环中进行选择,或者只是STATIC一个答案,那么您就不必担心Selected了,因为每次选择的内容都是相同的。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在动态下拉列表中选择一个选项 - How to make an option selected in dynamic dropdown list 如何使用引导程序进行下拉菜单,使选定的下拉菜单选项现在成为下拉菜单名称? - How to make a dropdown using bootstrap such the selected dropdown option now becomes the dropdown name? 如何使已选择的选项不在另一个 select 选项下拉 JAVASCRIPT/ANGULARJs 中显示 - How to make already selected option not display in another select option dropdown JAVASCRIPT/ANGULARJs 如何从下拉菜单中选择选项? - how to get selected option from dropdown menu? 如何在IE的下拉列表中验证所选选项 - How to verify the selected option in a dropdown in IE 如何在Django的下拉选择选项中插入按钮? - How to insert a button in a dropdown selected option in Django? 如何使用 ng-multiselect-dropdown 在 angular 中将选择选项设为默认选择? - How to make a select option as default selected in angular using ng-multiselect-dropdown? 在下拉列表中选择选项之前,如何隐藏单选按钮? - How do I make radio button hidden until a option is selected in dropdown? HTML &amp; Javascript - 选择下拉菜单选项时显示文本 - HTML & Javascript - make text appear when dropdown menu option selected 如何使 Bootstrap 下拉菜单显示在粘性表格标题的前面? - How to make a Bootstrap dropdown show up in front of sticky table headers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM