我有一个IE网页,其中包含以下代码:(我对此文章进行了简化)
<select class="form-control" id="areaSelect" onchange="blur()">
<option value="uno" selected="">Uno</option>
<option value="due" selected="">Due</option>
<option value="tre" selected="">Tre</option>
</select>
<input id="srch-term"></input>
使用以下命令,我可以将输入文本设置为“ foobar” :(有效!)
.getElementById("srch-term").value = "foobar"
在使用以下代码时,什么都没有发生:
.getElementById("areaSelect").click ' (it doesn't work: nothing happens)
.getElementById("areaSelect").value = "due" ' (it doesn't work: nothing happens)
即使在IE控制台中,最后两个命令也不起作用,没有错误也没有效果。
为什么? 可能是什么问题?
答案 0 :(得分:0)
您究竟想要什么,是否要将输入文本更改为与select相同的值? 请更好地解释。
答案 1 :(得分:0)
文本区域与下拉列表不同。要更改该选项,必须选择所需的索引并触发更改事件。您可以这样做:
'Needed variable
Dim nodeAreaSelect As Object
'Your code
'...
'Select "Due" from drop-down list
Set nodeAreaSelect = .getElementByID("areaSelect")
nodeAreaSelect.selectedIndex = 1 'Index from 0 to 2 in your example. Index 1 for "Due"
'Trigger change event of the drop-down list
Call TriggerEvent(browser.document, nodeAreaSelect, "change") 'Change browser.document to your html document
'The rest of your code
'...
以下触发事件的过程:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub