帮酷LOGO
0 0 评论
  • 显示原文与译文双语对照的内容
文章标签:切分  array  ARR  splitting  SPL  

介绍

这里代码将帮助你通过表单( 在屏幕上呈现或者存储在数据库中) 存储复选框或者单选按钮值。 如果窗体中有许多复选框/单选按钮,同样的NAME 和传递给窗体,这是非常有用的。 或者,如果要在一个数据字段中添加数据库中变量的所有值。

这段代码将向你展示如何使用ASP的SPLIT() 函数。

当你将多个项保存在一个变量中时,SPLIT() 函数很有用。 然后你可以简单地用 SPLIT() 函数分解该变量,以构建 array。 请参见示例代码:

对于任何问题,请随时询问我。

使用代码

<%'First declare a variable [myVariable]'It contains 3 values each of these are seperated'by a comma, (you may use any seperator)
myVariable = "myValue1, myValue2, myValue3"
'Make an array to store and break the variable'[Split] function do that work
myArray = Split(myVariable,",")
'now we can see logically our Variable have been broken 'into 3 parts as an Array 'something like: '*************** 'Logical Diagram '***************
' ------------------------' | myArray | Value |' ------------------------' | 0 | myValue1 |' | 1 | myValue2 |' | 2 | myValue3 |' ------------------------
'Split function brokes variable and makes Array'Array always start from 0 (zero) to last number'Now we need a loop to render array on screen
For i = LBound(myArray) TO UBound(myArray)
 Response.Write myArray(i) & "<BR/>"Next
'run this code you will see the result'what going on to the 上面 line?'See the following example. Loop was running 3 times, each times value for i= changed, read below...
'First Time Loop (view logical diagram)-------------'For i = 0'Response.Write myArray(0) it's equal to [myValue1]'Next-------------
'Second Time Loop (view logical diagram)-------------'For i = 1'Response.Write myArray(1) it's equal to [myValue2]'Next------------- 
'Third Time Loop (view logical diagram)-------------'For i = 2'Response.Write myArray(2) it's equal to [myValue3]'Next------------- 
'so it will loop thrice and array values will be print out on the screen%>

我们如何从 上面 示例中获益?

我们可以将这里变量设置为 Request.QueryString 或者 Request.Form:

'example'myVariable ="myValue1, myValue2, myValue3"

使用代码作为 QueryString:

myVariable = Request.QueryString ("itemName")

或者使用代码作为 Form 集合:

myVariable = Request.Form ("itemName")

如果请求的值不同但变量相同,这就是脚本帮助的地方。

现在,我们应该创建一个 窗体 :

<formaction="mypage.asp">my hobbies:<inputtype="radio"name="hobby"value="martial arts"/> Martial Arts <br/><inputtype="radio"name="hobby"value="programming"/> Programming <br/><inputtype="radio"name="hobby"value="books"/> books <br/><inputtype="submit"value="Go!"/></form>

获取关键字按下提交后,表单将发送如下所示的爱好:?

mypage.asp?hobby=martial arts&hobby=programming&hobby=books

那么,我们是否需要创建三个变量来捕获三个请求值? 当然不要,所以我们只能在一个变量中捕捉到一切: !

Dim hobby
hobby = Request.QueryString ("hobby")'now, if we will print 上面 variable on the screen it will display the result as:Response.Write hobby'Screen Result will be as =>>> martialarts,programming,books

现在,如何从一个变量中提取文本? 解决方案:通过 SPLIT() 函数轻松实现。 使 array 存储并中断变量。

'[Split] function do that workhobby = Split(Request.QueryString("hobby"), ",") 

SPLIT 函数有两个参数:

  • SPLIT ( variableName,我们需要在其中中断变量)

所以我们通过 QueryString 然后放置","。 SPLIT() 将通过删除","将变量分隔为 array。

变量变成了 array,如下所示:

hobby(0) = "martial arts"hobby(1) = "programming"hobby(2) = "books"

现在,打印这些值没有问题:

For i = LBound(hobby) TO UBound(hobby)
 Response.Write hobby(i) & "<BR/>"Next

而且,你可以轻松地在数据库中添加值,希望如此。

Have: )



文章标签:ARR  SPL  array  切分  splitting  

Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备05059198号-3  |  如果智培  |  酷兔英语