问题
I am new to classic ASP. What is wrong with the code below: If condition error, don't get it. Please help.
<select NAME="Priority" style="WIDTH:200px" Id="Priority">
<option value='0' <%= if(condition) then "selected" end%> 0 </option>
<option value='1' <%= if(condition) then "selected" end%> 1 </option>
<option value='2' <%= if(condition) then "selected" end%> 2 </option>
<option value='3' <%= if(condition) then "selected" end%> 3 </option>
</select>
回答1:
Should be :
<% if condition then response.write("selected") %>
For more info see here:
http://www.codefixer.com/tutorials/If_then_else.asp
回答2:
There is no in-line if function (note I didn't say statement) in VBScript. So I always have this in my toolbox:
function iif(siONo, SiRetval, NoRetval )
if SiONo then
iif = SiRetval
else
iif = NoRetval
end if
end function
Which allows you to do:
<option value='0' <%= iif(condition, "selected", "") %> 0 </option>
回答3:
If the select (Priority) is getting its data from a database then you could use a function. Is the page updating or inserting?
<select NAME="Priority" style="WIDTH:200px" Id="Priority">
<option value="0" <%= isSelected(Priority,"0") %>>0</option>
<option value="1" <%= isSelected(Priority,"1") %>>1</option>
<option value="2" <%= isSelected(Priority,"2") %>>2</option>
<option value="3" <%= isSelected(Priority,"3") %>>3</option>
</select>
Function isSelected(x,y)
if Cstr(x) = Cstr(y) then
isSelected = "selected=""Selected"""
else
isSelected = ""
end if
end Function
The item Priority would be a numeric field and have a default value assigned. Place the function snippet in a page that is used globally.
回答4:
You were missing if in 'end if' and there was a extra closing tag missing in after closing asp code block %>. Hope this works.
<select NAME="Priority" style="WIDTH:200px" Id="Priority">
<option value="0" <% if(condition) then Response.write("selected") end if %>>0</option>
<option value="1" <% if(condition) then Response.write("selected") end if %>>1</option>
<option value="2" <% if(condition) then Response.write("selected") end if %>>2</option>
<option value="3" <% if(condition) then Response.write("selected") end if %>>3</option>
</select>
来源:https://stackoverflow.com/questions/8663025/if-condition-not-working-in-classic-asp