Display alert based on the background color of a d

2019-07-23 17:40发布

问题:

A have a drop down list which is being populated by a database, certain options in the list have a different background colour according to whether they have been banned or not.

I would like to run a VB script which runs onChange which displays an alert if the user tries to select a user from the list which has a red background colour.

For Example:

Jim
Bob   ----> Red Background (Highlighted Red)
Dave
Tom

then if the user tries to select Bob then it will bring up an alert and then focus the select back to NULL.

Sub TestSub()
            frmCol = document.getElementById("frmNew").style.backgroundColor
            if frmCol = "Red" Then
            msgbox "This user is banned, please select another user!"
            End If
        End Sub

Hopefully that makes sense! Thank you for any help, very much appreciated. Ask if more details are needed!

回答1:

Something like this should do what you want:

For Each opt In document.getElementById("frmNew").options
  If opt.selected And opt.style.backgroundColor = "red" Then
    MsgBox "This user is banned."
  End If
Next

However, as @DanielCook said, just checking the actual name against a list of banned users would be a better approach, because the application logic is less shrouded that way:

Set bannedUsers = CreateObject("Scripting.Dictionary")
bannedUsers.Add "johnsmith", True
bannedUsers.Add "cmanson", True
...

For Each opt In document.getElementById("frmNew").options
  If opt.selected And bannedUser.Exists(opt.text) Then
    MsgBox "This user is banned."
  End If
Next