访问浏览器控制jQuery

时间:2016-10-06 21:41:43

标签: javascript jquery vba ms-access access-vba

我有一个带有网络浏览器控件的表单。我有一个函数设置下拉列表的值,然后调用jQuery更改函数:

Private Sub setDropDown(elemID As String, elemValue As Variant)

Dim elem As HTMLSelectElement
Dim i As Integer, win As Object

Set elem = Me.webBrowser.Object.Document.getElementById(elemID)
elem.Value = elemValue
Set win = Me.webBrowser.Object.Document.parentWindow
win.execScript "$('#" & elemID & "').change();"

End Sub

我在webBrowser_DocumentComplete事件中将其称为两个不同的下拉列表:

setDropDown "locationType", "1-4XB-2400"
setDropDown "areaType", "1-4XB-1958"

当此事件触发时,这些调用不起作用。然而,我还添加了一个命令按钮,并为click事件添加了完全相同的命令,这样可以正常工作。我尝试在DocumentComplete事件中更改焦点并添加各种长度的暂停。这似乎都不起作用。

如果它有助于选择来自JavaScript toolbox的类dynamicOptionList,并且我无法控制页面。此外,似乎我不能通过jQuery调用change()事件。

1 个答案:

答案 0 :(得分:0)

行。想出这个。问题是有一个脚本在页面加载后设置选择值。所以我会在documentcomplete事件中设置,然后它会被覆盖。暂停并不能解决这个问题。

解决方法是将onchange事件添加到javascript对象:

Public Sub setDropDown(elem As String, val As String)
    Dim sel As IHTMLSelectElement
    Dim head As IHTMLElement
    Dim script As IHTMLScriptElement
    Dim setVar As String, elemVar As String

    Set head = wb.Object.Document.getElementsByTagName("head")(0)
    Set script = wb.Object.Document.createElement("script")
    setVar = "_" & elem & "Set"
    elemVar = "_" & elem
    script.Text = "var " & elemVar & "= document.getElementById('" & elem & "'); " + _
        "var " & setVar & "=0; " + _
        "if (" & elemVar & "!= undefined) { " + _
            elemVar & ".setAttribute('onchange', function() { " + _
                "if (" & setVar & "==0) { " + _
                    elemVar & ".value = '" & val & "'; " + _
                    setVar & "= 1;" + _
                "}" + _
            "});" + _
        "};"
    head.appendChild script

End Sub