call vbscript from HTML event handler
I have a dropdown list that calls a vbscript.The issue is that when I try to get the value of the drop down list I get "object doesnt support property" or "method error".
<select onchange='callMe()' id='selectMe'>
<option value='1'>1</option>
<option value='3'&开发者_运维百科gt;3</option>
<option value='2'>2</option>
</select>
<SCRIPT LANGUAGE="VBScript">
Sub callMe()
MsgBox(selectMe.value)
End Sub
</SCRIPT>
Can anyone point me in the correct direction please
You can use W3Schools reference on the HTML DOM Select Object.
- Use selectMe.options
- Use selectMe.selectedIndex
When using VBScript, you will get implicit events base on the object's id so you don't need to explicitly call them (i.e. selectMe_onchange). I've reworked your example as follows:
<html>
<head>
<title>VBScript Select event</title>
<script LANGUAGE="VBScript">
Sub selectMe_onchange
MsgBox selectMe.options(selectMe.selectedIndex).text
End Sub
</script>
</head>
<body>
<select id='selectMe'>
<option value='1'>1</option>
<option value='3'>3</option>
<option value='2'>2</option>
</select>
</body>
</html>
you can do what Stephan Quan has posted as it does not matter where in page eg. head or body it is placed
in the following examples you would need to be in the body as it required for select element to be created prior to using
' if you just need to display
msgbox document.getElementById("selectMe").value
' if you need to do something with the value
dim somevariable
somevariable = document.getElementById("selectMe").value
in the second example somevariable would contain the value attribute from selected option
精彩评论