In the past two days, I have seen friends ask how to use script to operate various controls in HTML, and now I will summarize it myself. Due to project reasons, many programs are written in VBScript. Interested friends can rewrite them into JavaScript. Don’t forget to share them then, live.
OK, let’s get to the topic and let’s introduce it now:
Today we will introduce the use of VBScript to determine the number of selected check boxes and the value of the selected check box.
When generating a list, such as an order list, a check box may be placed in front of each order record to provide functions such as batch deletion and issuance. Generally, the value of each check box is the value of the primary key of the corresponding record, such as the order number. When performing functions such as deletion and release, you need to know how many check boxes are selected and what values these check boxes correspond to. The method is explained below.
The program is divided into three parts:
1. Determine whether the selected CheckBox is one or more than one.
The main idea is: when there are multiple check boxes with the same name but different IDs, for example, the check box control is named chkTest, then it will It is called in the form of an array. For example, there are three check boxes named chkTest and their IDs are chkTest1, chkTest2, and chkTest3. If you want to view the checked attributes of the three check boxes, you must write: chkTest(0).checked , chkTest(1).checked, chkTest(2).checked (the array starts from zero); when there is only one check box, chkTest.checked can be used directly. This determines that for different numbers (one or more), different methods can only be used to obtain check box information. Because when there are multiple controls with the same name and different IDs, the control has the property "control name.type", and this read-only property can be equal to any string, that is to say, control name.type = "any string" Constant is true. Of course, this is only when there is a control name.type attribute, otherwise it is always false, so that it can be distinguished whether the check box in the list is single or multiple. The procedure is as follows:
'************************************************ ***
'<function name>
' gfIs_ArrayTest(obj)
'<function>
' Determine whether the selected CheckBox is one or more than one
'<parameter>
' CheckBox object name
'<return value>
' true
' false
'<Remarks>
' 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
2. Check whether the selection of check boxes on the screen meets the requirements.
The so-called compliance means, for example, when deleting, select at least one record, when correcting, select and only select one record. As for the others, what Only 5 items can be selected, only 10 items can be selected, etc. Just change the following procedure to the following. Don’t forget what I just said, there are different ways to handle only one checkbox and multiple checkboxes.
'************************************************ *****
'<function name>
' gfChkCheckbox()
'<function>
' Check whether the selection of the Screen CheckBox meets the requirements
'<parameter>
' obj: corresponding checkbox control
'flag: Check rules: 0 can have multiple selected
' 1 Only one can be selected
' name: prompts the user with information about which control reported an error
'<return value>
' true
' false
'<Remarks>
' Created on Mar.15th.2004 by AITD
'************************************************ *****
function gfChkCheckbox(obj,flag,name)
Dim i
j
gfChkCheckbox = false
i = 0
j = 0
'If the object does not exist
if isnull(obj) then
msgbox "Please select an " + name + "." 'Please select an object
gfChkCheckbox = false
exit function
end if
'There is only one object
if not gfIs_ArrayTest(obj) then
if obj.checked = false then
msgbox "Please select an " + name + "." 'Please select an object
gfChkCheckbox = false
exit function
else
gfChkCheckbox = true
exit function
end if
end if
'If multiple objects exist
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
3. Obtain
the value
of the check box . For the convenience of later processing, important information is often saved in the value attribute of the check box for later access.The following program strings the value of the selected check box into a string using the separator "^|^" and returns it.
'************************************************
'<function name>
'gfGetCheckBoxValue(obj)
'<function>
'Get the value of the selected CheckBox
'<parameter>
' CheckBox object name
'<return value>
'The value of the selected CheckBox
'<Remarks>
' 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
gfGetCheckBoxValue = strValue
end function
is the method of processing check boxes in VBScript (occasionally used). These functions can be encapsulated into a common vbs file and referenced in the <head> tag area of html to make them universal ~ such as <head><script language=vbscript src=vbsChkBoxTool.vbs></script> </head>
Also note that in actual applications, there may not be a single record in the list generated based on the query conditions, that is to say, there is no check box control. If a control with such a name is still called, IE will An error will be reported. There was no good solution, so I wrote a program to bypass this situation without a checkbox control:
function sIsChkBoxExist()
on error resume next
err.clear
if isEmpty(document.frmOrderList.chkOrder) then
end if
if err.number <> 0 then
else
sIsChkBoxExist=true
end if
err.clear
end function
program, perform any operation on the predetermined control, such as isEmpty. If err.number>0 is captured, it means that the control does not exist. However, because there is on error resume next, no error will be prompted. This is also It determines whether the control exists.
Each time before calling the above three functions, call the sIsChkBoxExist function once to see if the corresponding control exists, so that there will be no errors.