2017-07-27 24 views
4

好吧,沒有輸入字段的HTML日期選擇器。使用VBA火更新事件

一個簡單的問題與困難的解決方案(目前)

有一個頁面一個往返日期選擇器。日期選取器沒有html輸入元素,但我可以使用InnerText訪問它。 有使用的innerText把往返日期,

問題沒有問題產生這樣:

當日期是手動進入,一個「形式」出現,則表明更新live用戶選擇日期,並僅顯示該日期範圍內的數據。當這樣做編程有「形式」

嘗試沒有任何活動更新至今:

Set document = objIE.document 

With document 
    .getElementsByClassName("controls").Item(0).Focus 
    .getElementsByClassName("select-daterange").Item(0).Focus 
    .getElementsByClassName("select-datepicker from").Item(0).Focus 
    .getElementsByClassName("date-label").Item(0).innerText = "June 1, 2017" 
    .getElementsByClassName("date-label").Item(1).innerText = "June 5, 2017" 
End With 

欣賞來自社區的任何指導!

這裏是什麼樣的日期選擇器看起來像一個示例:

No-input Date Picker

+0

IE11。我應該嘗試Firefox嗎? – user1

+0

如果你不能共享URL,那麼在頁面上使用了什麼'date-picker'?你能在這裏更具體嗎? – dee

+0

http://alloyui.com/examples/datepicker/像這樣。沒有輸入框,只選擇日期 – user1

回答

1

我已經證明了它是可以手動火從VBA使用fireevent Javascript事件。

這裏

Mozilla Dev Network - EventTarget.fireEvent

一些鏈接這裏是我的代碼。

Option Explicit 

Sub Test() 
    '* Tools->References->Microsoft HTML Object Library 
    '* Tools->References->Microsoft Internet Controls 

    Dim ie As SHDocVw.InternetExplorerMedium 
    Set ie = New SHDocVw.InternetExplorerMedium 


    ie.Visible = True 
    ie.navigate "n:\TestWebOnChangeManually.html" 

    Dim obj As Object 

    Stop '* i use this pause to dismiss Allow Block Content footer warning message 
    Dim dom As MSHTML.HTMLDocument 
    Set dom = ie.document 

    Dim textSource As MSHTML.HTMLInputElement 
    Set textSource = dom.getElementById("source") 

    textSource.innerText = "poked" 

    'https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/fireEvent 
    '* not sure what to place in second parameter 
    textSource.FireEvent "onchange", Nothing 


End Sub 

,並使用這個網站的測試文件

<html> 
<head/> 

<body> 

<input id="source" type="text" onChange="MyChange('source')"></input> 

<input id="someOtherControlToFocusSwitchTo" type="text" value="foo"></input> 


<script> 
    function MyChange(eleId) 
    { 
     if (eleId) 
     { 
      var ele = document.getElementById(eleId); 
      if (ele) { 
       alert(ele.value); 
      } 
     } else 
     { 
      alert("MyChange fired with no param"); 
     } 

    } 

</script> 
</body> 
</html> 

其他地方似乎有使用dispatchEvent一些代碼SO FireEvent and IE11複製,雖然我沒有嘗試這種不同的方法。這是基於在SO How can I trigger an onchange event manually?發現一些JavaScript VBA代碼本身是SO How to trigger event in JavaScript?

' Fire the onChange event... 
Dim objEvent 
Set objEvent = doc.createEvent("HTMLEvents") 
objEvent.initEvent "change", False, True 
e.dispatchEvent objEvent 

重複我希望這是夠你調試這個東西可能會非常棘手。

+0

VBA是爲Excel的VBScript是爲瀏覽器看起來你創建了一個不是我如何解釋問題的迴應,但我確實認爲這是解釋它的唯一有效方法。 – William

+0

@威廉,是我編輯的標籤。 –

+0

HTML/Js中沒有onchange元素/事件 – user1

相關問題