Display alert based on the background color of a drop down select option

北城余情 提交于 2020-01-16 04:02:48

问题


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


来源:https://stackoverflow.com/questions/17115877/display-alert-based-on-the-background-color-of-a-drop-down-select-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!