简体   繁体   English

如何通过EmbeddedWB.FillForm设置复选框的值? (DELPHI)

[英]How to set value for checkbox via EmbeddedWB.FillForm ? (Delphi)

how can i set value for a checkbox via FillForm method ? 如何通过FillForm方法为复选框设置值? I tried these but doesn't work : 我试过这些但不起作用:

  W.FillForm('Chkname', 'True');
  W.FillForm('Chkname', '1');
  W.FillForm('Chkname', '', 1);

Quite late, I know, but I'll try to answer this since it's a good question and since even the current version of the TEmbeddedWB doesn't have this feature implemented. 很晚,我知道,但我会尝试回答这个,因为这是一个很好的问题,因为即使是当前版本的TEmbeddedWB也没有实现这个功能。

However you can add your own function for doing this; 但是,您可以添加自己的功能来执行此操作; in the following example I'm using the interposed class of TEmbeddedWB where I overloaded the FillForm function with the version which supports check box and radio button filling. 在下面的示例中,我使用插入的TEmbeddedWB类,其中我使用支持复选框和单选按钮填充的版本重载了FillForm函数。

If you would like to set the check box or select some radio button call this version of function, where: 如果您想设置复选框或选择一些单选按钮,请调用此版本的功能,其中:

  • FieldName (string) - is the name of the element FieldName(string) - 是元素的名称
  • Value (string) - value of the element (can be empty, but in that case the first element of the FieldName will be set; web developers should use name value pairs IMHO) Value(string) - 元素的值(可以为空,但在这种情况下,将设置FieldName的第一个元素; Web开发人员应使用名称值对IMHO)
  • Select (Boolean) - if True, check box is checked or radio button selected 选择(布尔值) - 如果为True,则选中复选框或选中单选按钮

Here is the code: 这是代码:

uses
  EmbeddedWB, MSHTML;

type
  TEmbeddedWB = class(EmbeddedWB.TEmbeddedWB)
  public
    function FillForm(const FieldName, Value: string;
      Select: Boolean): Boolean; overload;
  end;

implementation

function TEmbeddedWB.FillForm(const FieldName, Value: string;
  Select: Boolean): Boolean;
var
  I: Integer;
  Element: IHTMLElement;
  InputElement: IHTMLInputElement;
  ElementCollection: IHTMLElementCollection;
begin
  Result := False;
  ElementCollection := (Document as IHTMLDocument3).getElementsByName(FieldName);
  if Assigned(ElementCollection) then
    for I := 0 to ElementCollection.length - 1 do
    begin
      Element := ElementCollection.item(I, '') as IHTMLElement;
      if Assigned(Element) then
      begin
        if UpperCase(Element.tagName) = 'INPUT' then
        begin
          InputElement := (Element as IHTMLInputElement);
          if ((InputElement.type_ = 'checkbox') or (InputElement.type_ = 'radio')) and
            ((Value = '') or (InputElement.value = Value)) then
          begin
            Result := True;
            InputElement.checked := Select;
            Break;
          end;
        end;
      end;
    end;
end;

And here a basic example of usage: 这里是一个基本的使用示例:

procedure TForm1.Button1Click(Sender: TObject);
var
  WebBrowser: TEmbeddedWB;
begin
  WebBrowser := TEmbeddedWB.Create(Self);
  WebBrowser.Parent := Self;
  WebBrowser.Align := alClient;
  WebBrowser.Navigate('http://www.w3schools.com/html/html_forms.asp');

  if WebBrowser.WaitWhileBusy(15000) then
  begin
    if not WebBrowser.FillForm('sex', 'male', True) then
      ShowMessage('Error while form filling occured...');
    if not WebBrowser.FillForm('vehicle', 'Bike', True) then
      ShowMessage('Error while form filling occured...');
    if not WebBrowser.FillForm('vehicle', 'Car', True) then
      ShowMessage('Error while form filling occured...');
  end;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM