2010-08-19 72 views
3

我怎樣才能通過FillForm方法設置複選框的值? 我想這可是不行的:如何通過EmbeddedWB.FillForm設置複選框的值? (德爾福)

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

哪裏你'FillForm'從何而來?我不記得這是一個標準的德爾福功能。它是什麼附加單元/庫? – 2010-08-20 18:49:32

+0

嵌入式Web瀏覽器:http://www.bsalsa.com – Kermia 2010-08-20 20:43:33

回答

3

比較晚,我知道,但我會盡力回答這個,因爲它是一個很好的問題,因爲即使TEmbeddedWB的當前版本沒有此功能已實施。

但是,您可以添加自己的功能來做到這一點;在下面的例子中,我使用了插入類TEmbeddedWB,其中我使用支持複選框和單選按鈕填充的版本過載了FillForm函數。

如果你想設置的複選框,或者選擇一些單選按鈕調用這個版本的功能,其中:

  • 字段名(字符串) - 是元素的名稱
  • 值(字符串) - 元素的值(可以是空的,但在這種情況下,將設置FieldName的第一個元素; Web開發人員應該使用名稱值對IMHO)
  • Select(布爾) - 如果爲True,複選框被選中或無線電選中按鈕

下面是代碼:

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; 

這裏使用的一個基本的例子:

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

這篇文章的主要觀點是,你不能使用'TEmbeddedWB.FillForm',因爲它設置了元素的'value'屬性不應該這樣做,因爲複選框和單選按鈕等元素在發送表單時具有用於構建名稱值對的值。 – TLama 2012-03-07 11:43:46