【求助】这种多判断的代码该如何用简单的方式去实现同样的功能啊?
If InStr(Response.Form("a"),"aa") <> 0 Then
Response.Cookies("a") = "前天"
ElseIf InStr(Response.Form("a"),"bb") <> 0 Then
Response.Cookies("a") = "今天"
ElseIf InStr(Response.Form("a"),"cc") <> 0 Then
Response.Cookies("a") = "明天"
ElseIf InStr(Response.Form("a"),"dd") <> 0 Then
Response.Cookies("a") = "后天"
……
End If
请问这样繁琐的判断代码该怎样写才简单、高效?例如用循环?那种方式较为占用资源少?
问题点数:0、回复次数:18Top
1 楼duoduobaba(避雷针)回复于 2004-11-03 01:15:55 得分 0
Response.Form("a")包含的值是排他的吗?就是说如果包含了aa是不是就不会包含别的了Top
2 楼mikecheers(热血男儿)回复于 2004-11-03 01:34:40 得分 0
Response.Form???没见过。request?Top
3 楼wanna51(wanna)回复于 2004-11-03 01:38:42 得分 0
function getname(key)
dim df
set df=server.createobject("scripting.dictionary")
df.add "aa","前天"
df.add "bb","今天"
df.add "cc","明天"
df.add "dd","后天"
'想要更多就照着添
if df.exists(key) then
getname=df.item(key)
else
getname="N天"
end if
end function
用的时候就是Response.Cookies("a") =getname(Response.Form("a"))Top
4 楼wanna51(wanna)回复于 2004-11-03 01:40:18 得分 0
哎呀,你用的InStr啊,那你说用do while吧。Top
5 楼naiveteBOY()回复于 2004-11-03 02:26:31 得分 0
回复duoduobaba(避雷针) :不是的排他的。"aa"只是Request.From("a")里的一部分内容而已。
回复mikecheers(热血男儿) :不好意思,偶写错了,不是Response.From("a"),是Request.From("a")才对。
回复wanna51(恢恢):谢谢,偶没用过scripting.dictionary。不太明白这个对象属性的用法。请问用数组可以吗?另外用循环用do while好还是用For好呢?
Top
6 楼duoduobaba(避雷针)回复于 2004-11-03 02:57:18 得分 0
不是排他的就
str=replace(replace(replace(replace(Request.From("a"),"aa","前天"),"bb","今天"),"cc","明天"),"dd","后天")
Response.Cookies("a")=str
取回Cookies的时候用","split之后要trim一下
Top
7 楼duoduobaba(避雷针)回复于 2004-11-03 02:57:50 得分 0
不会还有大后天大大后天吧Top
8 楼naiveteBOY()回复于 2004-11-03 03:47:16 得分 0
有的,今天、明天、后天……这种有二三十个啊。
这样行不?
A = Split("aa,bb,cc,dd,ee",",")
B = Split("前天,今天,明天,后天,大后天",",")
For i = 0 To UBound(A)
If InStr(Response.Form("a"),"A(i)") <> 0 Then
Response.Cookies("a") = "B(i)"
End If
Next
请问这样行吗?偶试了下,好像不行吔。是否语法不对或那里错了?Top
9 楼naiveteBOY()回复于 2004-11-03 15:36:29 得分 0
顶Top
10 楼jingxiaoping(我知道你今天没有穿内衣,因为我看到了极其突出的两点)回复于 2004-11-03 15:45:50 得分 0
我在想会不会还有“大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大大后天”呢。
建议不用这种方法显示。Top
11 楼duoduobaba(避雷针)回复于 2004-11-03 16:03:16 得分 0
循环里Response.Cookies("a") = "B(i)"最后Cookies("a") 里面只有1个值
Top
12 楼songyanjun(songyanjun)回复于 2004-11-03 16:45:17 得分 0
1. Dictionary对象的成员概要
表5-2和表5-3列出了Dictionary对象的属性和方法及相应的说明。
当增加一个键/条目对时,如果该键已存在;或者删除一个键/条目对时,该关键字/条目对不存在,或改变已包含数据的Dictionary对象的CompareMode,都将产生错误。
表5-2 Dictionary对象的属性和说明
属 性 说 明
CompareMode (仅用于VBScript)设定或返回键的字符串比较模式
Count 只读。返回Dictionary里的键/条目对的数量
Item(key) 设定或返回指定的键的条目值
Key(key) 设定键值
表5-3 Dictionary对象的方法和说明
方 法 说 明
Add(key,item) 增加键/条目对到Dictionary
Exists(key) 如果指定的键存在,返回True,否则返回False
Items() 返回一个包含Dictionary对象中所有条目的数组
Keys() 返回一个包含Dictionary对象中所有键的数组
Remove(key) 删除一个指定的键/条目对
RemoveAll() 删除全部键/条目对
2. 对Dictionary中增加和删除条目
一旦得到一个新的(空的)Dictionary,可以对其添加条目,从中获取条目以及删除条目:
‘ In VBScript:
objMyData.Add “MyKey”, “MyItem” ‘Add Value MyItem with key MyKey
objMyData.Add “YourKey”, ”YourItem” ‘Add value YourItem with key YourKey
blnIsThere = objMyData.Exists(“MyKey”) ‘Returns True because the item exists
strItem = objMyData.Item(“YourKey”) ‘Retrieve value of YourKey
strItem = objMyData.Remove(“MyKey”) ‘Retrieve and remove YourKey
objMyData.RemoveAll ‘Remove all the items
在JScript中,等价的代码为:
// In JScript;
objMyData.Add (‘MyKey’, ‘MyItem’); //Add Value MyItem with key MyKey
objMyData.Add (‘YourKey’, ‘YourItem’); //Add value YourItem with key YourKey
var blnIsThere = objMyData.Exists(‘MyKey’); //Returns True because the item exists
var strItem = objMyData.Item(‘YourKey’); //Retrieve value of YourKey
var strItem = objMyData.Remove(‘MyKey’); //Retrieve and remove YourKey
objMyData.RemoveAll(); //Remove all the items
3. 修改键或条目的值
可以通过修改键的值,或通过修改与特定的键关联的条目的数据,来改变存储在Dictionary内的数据。下面的代码改变键为MyKey的条目中的数据。
ObjMyData.Item(“MyKey”) = “NewValue” ‘ In VBScript
ObjMyData.Item(‘MyKey’) = ‘NewValue’; // In JScript
如果指定的键在Dictionary未找到,将在Dictionary中创建一个以MyKey为键,以New Value为其条目值的新的键/条目对。有意思的是,如果使用一个不存在的键来检索条目,不仅得到一个空的字符串(这是可以想到的),而且还在Dictionary里添加一个新的键/条目对,键即是指定的键,但条目的数据为空。
可以使用Key属性仅改变键的值而不改变与之对应的条目的数据。将一个已存在的键MyKey改变为MyNewKey,可以用:
objMyData.Key(“MyKey”) = “MyNewValue” ‘ In VBScript
objMyData.Item(‘MyKey’) = ‘MyNewValue’; // In JScript
如果指定的键未找到,则产生运行期错误。
4. 设置比较模式
Dictionary的CompareMode属性仅适用于VBScript,不能在JScript中使用。当比较字符串键时,允许指定比较的方式。两个允许的值为BinaryCompare(0)和TextCompare(1)。BinaryCompare(0)为二进制数对照(即区分大小写);TextCompare(1)为文本对照(即不区分大小写)。
5. 遍历Dictionary
研究Dictionary时,有两个方法和一个属性需要特别注意,它们允许我们遍历存储在Dictionary里的所有键/条目对。Items方法用一个一维数组的形式返回Dictionary里所有的条目数据,而keys方法用一个一维数组返回所有已存在的键值。可以使用Count属性得到键或条目的数量。
例如,可以使用下列代码得到名称为objMyData的Dictionary中所有的键和条目值。注意,虽然Count属性保存了在Dictionary里的键/条目数量,但VBScript和JScript的数组总是从下标0开始的。因此,数组下标应从0到Count-1。
‘In VBScript:
arrKeys = objMyData.Keys ‘Get all the keys into an array
arrItems = objMyData.Items ‘Get all the items into an array
For intLoop = 0 To objMyData.Count –1 ‘Iterate through the array
StrThisKey = arrKeys(intLoop) ‘This is the key value
StrThisItem = arrItems(intLoop) ‘This is the item (data) value
Next
// In JScript
// Get VB-style arrays using the Keys() and Items() methods
var arrKeys = new VBArray(objMyData.Keys()).toArray();
var arrItems = new VBArray(objMyData.Items()).toArray();
for (intLoop = 0; intLoop < objMyData.Count; intLoop++) {
// Iterate through the arrays
strThisKey = arrKeys[intLoop]; // This is the key value
strThisItem = arrItems[intLoop]; // This is the item (data) value
}
在VBScript里也可以使用For Each … Next语句完成同样的功能:
‘ Iterate the dictionary as a collection in VBScript
For Each objItem in arrItems
Response.Write objItem & “ = “ & arrItems(objItem) & “<BR>”
Next
Top
13 楼naiveteBOY()回复于 2004-11-07 17:54:22 得分 0
顶~Top
14 楼naiveteBOY()回复于 2004-11-08 19:17:05 得分 0
继续问这个~Top
15 楼duoduobaba(避雷针)回复于 2004-11-09 23:29:08 得分 0
最终写入Cookies("a")的是什么?Top
16 楼naiveteBOY()回复于 2004-11-11 02:13:59 得分 0
回楼上的:
“今天”、“明天”、“后天”或者“大后天”……
的其中一个。Top
17 楼duoduobaba(避雷针)回复于 2004-11-11 03:34:16 得分 0
Response.Form("a")不是唯一的那意思是Cookies设的值是最后的一个值吗?
为什么Form中的a的value不直接设置为"今天","明天","后天".....呢?
纳闷
Top
18 楼naiveteBOY()回复于 2004-11-12 05:08:46 得分 0
回楼上的,不能这样设置哟,因为Form("a")是供用户填写的。Top




