How to initiate on change event when selecting item from drop down list?

主宰稳场 提交于 2019-12-11 05:31:34

问题


I'm trying to trigger "on change" event upon selecting currency value from drop-down menu. This would update the mini spreadsheet on that page reflecting the value of the currency. However when changing the value through VBA jscript is not being triggered. I'm using IE to do this.

  Set objIE = New InternetExplorer
    objIE.Visible = True
    objIE.navigate "https://www.reuters.com/markets/currencies"
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    'Settlement Currency is Hard Coded to GBP
    SettCcy = "GBP"
    'Set Expression Currency
    expCcy = "EUR"
    'Set Target Currency
    targetCcy = "USD"

    On Error GoTo ResetIE

    If expCcy <> SettCcy And expCcy <> targetCcy Then

        'Set the rate refresher
        Set clicker = objIE.document.getElementsByClassName("CurrencyCalculator-currency-swap-2yw2I")(0)

        'Set the Expression Currency
        For Each o In objIE.document.getElementsByTagName("select")(1) 'Sets Expression Currency
            If o.Value = expCcy Then 
                o.Selected = True
                o.FireEvent "onchange"
                Exit For
            End If
        Next

All I need is that upon changing the value through VBA the page would update the same way as it would when doing it manually.


回答1:


Please try to use the dispatchEvent method to dispatch the change event to the select element, then trigger the onchange event.

Sample code as below (you could refer to it and modify it, it works well on my side):

Public Sub ClickTest()

    Dim  ie As Object, evtChange As Object

    Dim item As Object

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate2 "<the website url>"

        While .Busy Or .readyState <> 4: DoEvents: Wend

        Set evtChange = .Document.createEvent("HTMLEvents")
        evtChange.initEvent "change", True, False

        'get the select element. Please note the index, it is starting from 0.
        Set item = ie.Document.getElementsByTagName("select")(0)

        expCcy = "EUR"

        'Set the Expression Currency
        For Each o In item 'Sets Expression Currency
            If o.Value = expCcy Then
                o.Selected = True
                o.dispatchEvent evtChange
                Exit For
            End If
        Next        
    End With
End Sub

The website resource like this:

<select id="ddlCurrency" onchange="ddlCurrencyChange()" >
    <option value="GBP">GBP</option>
    <option value="EUR">EUR</option>
    <option value="USD">USD</option>
</select>

<script>

    function ddlCurrencyChange() {
        document.getElementById("result").innerHTML += "change event triggered... <br /> selected value is " + document.getElementById("ddlCurrency").value + "<br/>";
    }
</script>


来源:https://stackoverflow.com/questions/58183046/how-to-initiate-on-change-event-when-selecting-item-from-drop-down-list

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