如何调用“选择”的OnChange事件? (Delphi - WebBrowser)

时间:2010-09-16 08:09:38

标签: html delphi combobox browser

我正在使用Delphi和WebBrowser组件来导航html页面。该页面有一个Combobox。有没有办法调用OnChange事件?

ComboBox是这样的:

<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;

但它只改变了价值。

2 个答案:

答案 0 :(得分: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;

了解更多信息,请查看这篇优秀文章

答案 1 :(得分: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;