首先我們看這樣一段代碼:
<html>
<head><title>一個簡單首頁</title>
<script language="vbscript">
<!--
sub button1_onclick
msgbox "歡迎光臨"
end sub
-->
</script>
</head>
<body>
<h3>一個簡單首頁</h3><hr>
<form><input name="button1" type="button" value="單擊此處"></form>
</body>
</html> 
這實現的是 當點擊按鈕時,彈出消息框,顯示 歡迎光臨
其中 
sub 定義一個過程,過程名包含兩部分:
button1 為按鈕名(從<input> 標記中的 name 屬性獲取)
onclick 是事件名,即button1的onclick事件其中兩部分用(_)連接
合起來實現的是,單擊按鈕,internet explorer 查找并運行相應的事件過程,即 button1_onclick
<input name="button1" type="button"
value="單擊此處" onclick='msgbox "歡迎光臨"'>
函數調用包含在單引號中,msgbox 函數的字符串包含在雙引號中。只要用冒號 (:) 分隔語句,就可以使
用多條語句。
<script language="vbscript" event="onclick" for="button1">
<!--
msgbox "歡迎光臨"
-->
</script>
這種方法在<script> 標記指定了事件和控件,所以不需要再用 sub 和 end sub 語句
進一步實現簡單驗證
<html>
<head><title>簡單驗證</title>
<script language="vbscript"> 
<!--
sub button1_onclick
dim theform
set theform = document.validform
if isnumeric(theform.text1.value) then
if theform.text1.value < 1 or theform.text1.value > 10 then
msgbox "請輸入一個 1 到 10 之間的數字。"
else
msgbox "謝謝。"
end if
else
msgbox "請輸入一個數字。"
end if
end sub
-->
</script>
</head>
<body>
<h3>簡單驗證</h3><hr>
<form name="validform">
請輸入一個 1 到 10 之間的數字:
<input name="text1" type="text" size="2">
<input name="button1" type="button" value="提交">
</form>
</body>
</html>
這個文本框與 vbscript 頁面的簡單樣例中文本框的 value 屬性被用于檢查輸入值。要使用文本框的 
value 屬性,代碼必須引用文本框的名稱。
每次引用文本框時都應寫出全稱,即 document.validform.text1。但是,當多次引用窗體控件時,可以
按照以下步驟操作:首先聲明一個變量,然后使用 set 語句將窗體 document.validform(form的id)
賦給變量 theform,這樣就能使用 theform.text1 引用文本框。常規的賦值語句(例如 dim)在這里無
效,必須使用 set 來保持對對象的引用。
進一步實現 驗證后將數據傳遞回服務器
<html>
<head><title>簡單驗證</title>
<<script language="vbscript"> 
<!--
sub button1_onclick
dim theform
set theform = document.validform
if isnumeric(theform.text1.value) then
if theform.text1.value < 1 or theform.text1.value > 10 then
msgbox "請輸入一個 1 到 10 之間的數字。"
else
msgbox "謝謝。"
theform.submit 
end if
else
msgbox "請輸入一個數字。"
end if
end sub
-->
</script>
</head>
<body>
<h3>簡單驗證</h3><hr>
<form name="validform" action="要提交到的頁">
請輸入一個 1 到 10 之間的數字:
<input name="text1" type="text" size="2">
<input name="button1" type="button" value="提交">
</form>
</body>
</html>
sub中 theform.submit 一句指出將form的內容上傳到服務器端
有以下幾點需要注意,我在測試的時候如果將name="button1" 改為name="submit"程序出錯,原因可能是
因為submit是一個保留字
同樣,如果type="button" 改為type="submit"那么不管驗證結果如何,數據都將上傳到服務器端。
新聞熱點
疑難解答