选择下拉列表中的经典ASP - IF语句

时间:2013-06-13 04:56:26

标签: html asp-classic

我想知道是否有人可以为我排序。

我有以下脚本

strInput = "<select name=""winner"">" &_
    "<option></option>" &_
    "<option value=""1"">1st</option>" &_
    "<option value=""2"">2nd</option>" &_
    "<option value=""3"">3rd</option>" &_
    "<option value=""4"">4th</option>" &_
"</select>"

如何在选项中插入'if'语句以检查值,然后默认选择该选项

if intVal = 1 then
    selected="selected"
End If

2 个答案:

答案 0 :(得分:0)

"<option value=""1"" " & iif(intVal = 1, " ""Selected"" ", " ") & ">1st</option>"

答案 1 :(得分:0)

尝试

strInput = "<select name=""winner"">"
strInput = strInput &  "<option></option>"
strInput = strInput &  "<option value=""1"""
If intVal = 1 Then
    strInput = strInput &  " selected"
End If
strInput = strInput &  ">1st</option>"
strInput = strInput &  "<option value=""2"""
If intVal = 2 Then
    strInput = strInput &  " selected"
End If
strInput = strInput &  ">2st</option>"
strInput = strInput &  "<option value=""3"""
If intVal = 3 Then
    strInput = strInput &  " selected"
End If
strInput = strInput &  ">3st</option>"
strInput = strInput &  "<option value=""4"""
If intVal = 4 Then
    strInput = strInput &  " selected"
End If
strInput = strInput &  ">4st</option>"
strInput = strInput &  "</select>"

Function IsSelected(optionValue, defaultValue)
    If optionValue = defaultValue Then
        IsSelected = " selected"
    Else
        IsSelected = ""
    End If
End Function

strInput = "<select name=""winner"">" &_
    "<option></option>" &_
    "<option value=""1""" & IsSelected(1,intVal) & ">1st</option>" &_
    "<option value=""2""" & IsSelected(2,intVal) & ">2nd</option>" &_
    "<option value=""3""" & IsSelected(3,intVal) & ">3rd</option>" &_
    "<option value=""4""" & IsSelected(4,intVal) & ">4th</option>" &_
"</select>"