我必须从SQL填充值到下拉框。经典ASP是我需要使用的语言。我能够填充它;但是,我无法在下拉列表中保留所选值。我在循环中的“选项值”之后保留了“选定”值,但执行此操作后的问题是:所有先前选项继续加载(选择)下拉,直到我选择的那个进入列表。任何人都可以建议/评论哪里/我错过了什么?
Categories:<select name="menu" onChange="if(options[selectedIndex].value != 0){location='test.asp?list=bybrand&brandID=' + options[selectedIndex].value + '', exit=false;}">
<OPTION value=0 selected>-- SELECT --</OPTION>
<% dim rs
SQL = "SELECT DISTINCT(brand) as brand FROM tblprdt"
SET rs=objConn.Execute(SQL)
IF NOT (rs.BOF and rs.EOF) THEN
WHILE NOT rs.EOF
Response.Write "<option value=""" & replace(rs("brand")," & ","@") & """ selected >" & rs("brand") & "</option>"
rs.MoveNext
WEND
ELSE
Response.Write "<option >No categories available</option>"
END IF
rs.close
SET rs = nothing
%>
</select>
答案 0 :(得分:1)
假设BrandId值对应于表tblprdt中的关键字段,该字段也称为BrandId,请将查询更改为
SQL = "SELECT BrandId, brand FROM tblprdt"
然后再
WHILE NOT rs.EOF
if cint(rs("BrandId")) = cint(Request.Querystring("BrandId")) then
Response.Write "<option value=""" & replace(rs("BrandId")," & ","@") & """ selected >" & rs("brand") & "</option>"
else
Response.Write "<option value=""" & replace(rs("BrandId")," & ","@") & ">" & rs("brand") & "</option>"
end if
rs.MoveNext
WEND
编辑我刚刚测试了你的例子中的js,它只传递rs("brand")
的值,所以它更简单。保持查询不变,然后使用
WHILE NOT rs.EOF
if replace(rs("BrandId")," & ","@") = Request.Querystring("BrandId") then
Response.Write "<option value=""" & replace(rs("Brand")," & ","@") & """ selected >" & rs("brand") & "</option>"
else
Response.Write "<option value=""" & replace(rs("brand")," & ","@") & ">" & rs("brand") & "</option>"
end if
rs.MoveNext
WEND