Using VBScript to manipulate the HTML check box (checkbox) control

Source: Internet
Author: User
Tags exit constant
vbscript| check box | Control This two days to see a friend asked the HTML inside the various controls how to use the script to operate, now I also summed up. Because of the project reason, many programs are written in VBScript, interested friends can be changed into JavaScript, then don't forget to share Ah, alive.

OK, cut to the theme, now introduce:

Today I'll explain the number of checked boxes and the values of the selected check boxes using VBScript.

When a list is generated, such as a list of order orders, a check box may be placed in front of each order to provide functionality such as bulk deletion, distribution, etc. Generally, the value of each check box is the value of the primary key that should be recorded, such as the order number. In the implementation of the deletion, distribution, and other functions, you should know that a few check boxes, these checkboxes correspond to what values, the following is the method.

The program is divided into three parts:

First, judge whether the selected checkbox is one or more

The main idea is that when there are multiple check boxes with the same name, but the ID is different, the example CheckBox control name is Chktest, then it will be called as an array, for example, there are three check boxes named Chktest,id respectively is CHKTEST1,CHKTEST2,CHKTEST3, The checked property for the three check boxes is written like this: Chktest (0). Checked, Chktest (1). Checked, Chktest (2). Checked (array zero-based); And when there is only one check box, Chktest.checked can be used directly. This determines that for a number of different (one or more), only a different method can be used to get the check box information. Because when there are multiple controls with different IDs with the same name, the control has the property "control name. Type", and this read-only property can be equal to any string, that is, the control name. Type = "Any string" constant is true. Of course, this is only true for the property of the control name. Type, otherwise the constant is false, so that the check box in the list is single or multiple. The procedure is as follows:

'*************************************************
' < function name >
' Gfis_arraytest (obj)
' < function >
' To determine whether a selected checkbox is one or more
' < parameters >
' CheckBox object Name
' < return value >
' True
' False
' < notes >
' Created on mar.15th.2004 by AITD
'*************************************************
function Gfis_arraytest (obj)
On Error Resume Next
If Obj.type = "Flag" Then
Gfis_arraytest = True
Else
Gfis_arraytest = False
End If
End Function

Second, check the screen check box to choose whether to meet the requirements

The so-called whether to meet the requirements of the meaning is, for example, the deletion of the time to choose at least one record, the correct time to choose and can only select a record, as for the other, what can only choose 5, can only choose 10, and so on, the following procedures to change the following line. Don't forget what you just said, only one check box is different from the one with multiple check boxes.

'******************************************************
' < function name >
' Gfchkcheckbox ()
' < function >
' Check the screen checkbox to see if the selection meets the requirements
' < parameters >
' obj: corresponding checkbox control
' Flag: Check rule: 0 can have multiple selected
' 1 can only have one being selected
' Name: Information that prompts the user for an error in the control
' < return value >
' True
' False
' < notes >
' Created on mar.15th.2004 by AITD
'******************************************************
function Gfchkcheckbox (obj,flag,name)
Dim I
Dim J

Gfchkcheckbox = False
i = 0
j = 0
' If the object does not exist
If IsNull (obj) Then
MsgBox "Please select a + name +". "' Please select an object
Gfchkcheckbox = False
Exit function
End If

' Object has only one
If not gfis_arraytest (obj) Then
If obj.checked = False Then
MsgBox "Please select a + name +". "' Please select an object
Gfchkcheckbox = False
Exit function
Else
Gfchkcheckbox = True
Exit function
End If
End If

' If more than one object exists

For i = 0 to (obj.length-1)
If obj (i). Type = "checkbox" Then
If obj (i). Checked = True Then
j = j + 1
End If
End If
Next

If j = 0 Then
MsgBox "Please select + Name +". "' Please select an object
Gfchkcheckbox = False
Exit function
End If
If J = 1 Then
Gfchkcheckbox = True
Exit function
End If
If J > 1 Then
If flag = 1 Then
MsgBox "Only one" + name + "can be selected." "' Only one object can be selected
Gfchkcheckbox = False
Exit function
Else
Gfchkcheckbox = True
Exit function
End If
End If

End Function

Third, the value of the check box is obtained

For the convenience of later processing, the important information is often kept in the Value property of the check box for later use. The following program ^|^ the value of the selected check box into a string and returns with the spacer character "".

'*************************************************
' < function name >
' Gfgetcheckboxvalue (obj)
' < function >
' Gets the value of the selected checkbox
' < parameters >
' CheckBox object Name
' < return value >
' The value of the selected checkbox
' < notes >
' Created on mar.15th.2004 by AITD
'*************************************************
function Gfgetcheckboxvalue (obj)

Dim strvalue
Dim intcounter
Dim i

strvalue = ""
intcounter = 0

' If the object does not exist
If IsNull (obj) Then
Gfgetcheckboxvalue = strvalue
Exit function
End If

' If the object is a
If not gfis_arraytest (obj) Then
If obj.checked = False Then
Gfgetcheckboxvalue = strvalue
Exit function
Else
Gfgetcheckboxvalue = Obj.value
Exit function
End If
End If

' If the object is multiple
For i = 0 to Obj.length-1
If obj (i). Checked = True Then
If intcounter > 0 Then
strvalue = strvalue & "^|^" & CStr (obj (i). Value)
Else
strvalue = CStr (obj (i). Value)
End If
intcounter = intcounter + 1
End If
Next



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.