2010-09-16 41 views
1

我使用Delphi和WebBrowser組件來瀏覽html頁面。該頁面有一個Combobox。有什麼辦法可以調用OnChange事件嗎?如何調用「選擇」的OnChange事件? (Delphi - WebBrowser)

組合框是這樣的:

<select name="comboname" onchange="Some Javascript codes"> 

而且,我已經使用這個代碼:

function TFrmMain.SetComboboxValue(WB: TEmbeddedWB; 
    SelectName, ItemName: string): Boolean; 
var 
    iForms, iFormItems, iSelectItems: Word; 
    FormItem: OleVariant; 
begin 
    Result := false; 
    for iForms := 0 to WB.OleObject.Document.Forms.length - 1 do 
    begin 
    FormItem := WB.OleObject.Document.Forms.item(iForms); 
    for iFormItems := 0 to FormItem.length - 1 do 
    begin 
     if (FormItem.item(iFormItems). type = 'select-one') and SameText 
     (FormItem.item(iFormItems).Name, SelectName) then 
     begin 
     for iSelectItems := 0 to FormItem.item(iFormItems).Options.length - 1 do 
     begin 
      if SameText(FormItem.item(iFormItems).Options.item(iSelectItems) 
       .Text, ItemName) then 
      begin 
      FormItem.item(iFormItems).SelectedIndex := iSelectItems; 
      Result := true; 
      Break; 
      end; 
     end; 
     end; 
    end; 
    end; 
end; 

但它只能改變數值。

回答

4

執行onchange事件中,你可以使用execScript方法

檢查該樣本

uses 
    MSHTML; 

var 
    Doc: IHTMLDocument2;  
    HTMLWindow: IHTMLWindow2;   
begin 
    Doc := WebBrowser1.Document as IHTMLDocument2; 
    if not Assigned(Doc) then 
    Exit; 
    HTMLWindow := Doc.parentWindow; 
    if not Assigned(HTMLWindow) then 
    Exit; 

    HTMLWindow.execScript('yourfunctioname()', 'JavaScript'); 
end; 

更多信息檢查這個優秀的文章

+0

謝謝你,更簡單的方法? :d – Kermia 2010-09-16 08:58:16

0

受到迴應的啓發。 NET一直在使用下面的結構:

FrameSet Document Elements Item Name Value Change ; 
EWB.OleObject.Document.Frames.Item('mainFrame').Document.Forms.Item('invoiceForm').Elements.Item('inputname').Value:= '123456'; 

FrameSet Document Elements Items Lenth; 

EWB.OleObject.Document.Forms.Item('invoiceForm').Elements.Length;