将vba中的值插入到html列表中



我已经能够使用vba的.value方法处理列表中的数据。但是,当尝试更新特定值时,它会将该值填充为空白,不允许我更改它。我试图将日期推送到该字段中,但该日期取自列表旁边的日历,该列表将日期放在日历的框中。当使用dom资源管理器时,我只需更改选项值,它就会更改字段中的日期。当我使用value方法更改相同的选项时,它会将字段填充为空白。有什么想法吗?

页面的HTML代码

带有日历的列表图片

我已经能够使用以下内容更改值:

IE.Document.getElementsByTagName("select")(27).Value = _
    "Today@javascript:gs.daysAgoStart(0)@javascript:gs.daysAgoEnd(0)"

到已经存在的任何值,但即使它接受日期,也不能将其更改为日期。例如:

IE.Document.getElementsByTagName("select")(27).Value = "2016-11-04"

将列表留空,如同未选择任何内容一样。除非我输入的日期已经是从日历中提取的列表的一部分,否则我无法更改日期。

要选择选项,请使用selectedIndex属性。要向select元素添加新的option元素,请先使用doc.createElement("option")创建它,然后在select元素上调用add方法。HTH-

Option Explicit
' Add reference to Microsoft Internet Controls (SHDocVw)
' Add reference to Microsoft HTML Object Library
Sub NewOptionDemo()
    Dim ie As SHDocVw.InternetExplorer
    Dim doc As MSHTML.HTMLDocument
    Dim url As String
    url = "file:///c:/Temp/main.html"
    Set ie = New SHDocVw.InternetExplorer
    ie.Visible = True
    ie.navigate url
    While ie.Busy Or ie.readyState <> READYSTATE_COMPLETE
        DoEvents
    Wend
    Dim htmlCombobox As MSHTML.HTMLSelectElement
    Set doc = ie.document
    Set htmlCombobox = doc.querySelector("select[id='select1']")
    If (htmlCombobox Is Nothing) Then
        MsgBox "No combobox with id 'select1' was found on the page."
    Else
        ' select first option
        htmlCombobox.selectedIndex = 0
        ' create new option, add this option to combobox and select it
        Dim newOption As HTMLOptionElement
        Set newOption = doc.createElement("option")
        newOption.Value = "new-option"
        newOption.Text = "New option"
        htmlCombobox.Add newOption
        htmlCombobox.selectedIndex = 3
        ' chnge value and text of first option and select it again
        htmlCombobox.Item(0).Value = "new-value-for-first-item"
        htmlCombobox.Item(0).Text = "new value for first item"
        htmlCombobox.selectedIndex = 0
    End If
    ie.Quit
    Set ie = Nothing
End Sub

main.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
</head>
<body>
    <select id="select1">
        <option value="option1">Option 1</option>
        <option value="option2">2016-11-04</option>
        <option value="option3" selected="selected">Option 3</option>
    </select>
</body>
</html>

相关内容

  • 没有找到相关文章

最新更新