-1

我有以下 Delphi 代码来填写 TWebBrowser 上的表格:

procedure SetFieldValue(theForm: IHTMLFormElement; const fieldName: string; const newValue: string);
   var
     field: IHTMLElement;
     inputField: IHTMLInputElement;
     selectField: IHTMLSelectElement;
    textField: IHTMLTextAreaElement;
begin
  field := theForm.Item(fieldName, '') as IHTMLElement;
  if Assigned(field) then
  begin
    if field.tagName = 'INPUT' then
    begin
      inputField := field as IHTMLInputElement;
      // Make the change below to catch checks and radios.
      if (inputField.type_ = 'checkbox') or (inputField.type_ = 'radio') then
      begin
        if newValue = 'Y' then
          inputField.checked := True
        else
          inputField.checked := False;
      end
      else
        inputField.value := newValue;
    end
    else if field.tagName = 'SELECT' then
    begin
      selectField := field as IHTMLSelectElement;
      selectField.value := newValue;
    end
    else if field.tagName = 'TEXTAREA' then
    begin
      textField := field as IHTMLTextAreaElement;
      textField.value := newValue;
    end;
  end;
end;

HTML 源代码类似于以下示例:

<select name="xosid">
   <option value="" selected>-- choose one --</option>
   <option value="first">This is one</option>
   <option value="second">Another</option>
</select>

我想修改上述函数,以便能够在不知道值的情况下从下拉列表中选择“另一个”...

有什么建议么?

提前致谢,

佐尔特

4

2 回答 2

0

能够在不知道值的情况下从下拉列表中选择“另一个”

那么按索引呢?选项编号 2(从零开始) ?

让我们从在 Google 中搜索数据类型开始

第一个链接是Microsoft Internet Explorer 中该数据类型的 MSDN 规范。在右侧,您可以看到selectedIndex: Integer属性。

所以代码就像

else if field.tagName = 'SELECT' then
    begin
      selectField := field as IHTMLSelectElement;
      selectField.selectedIndex := 2;
    end

回到谷歌,我的第三个链接是使用该属性的现成源代码示例。

于 2013-07-19T13:27:58.967 回答
0
    selectField.value := (selectField.item(selectField.length-1,EmptyParam) as IHTMLOptionElement).value;
于 2013-07-20T08:14:59.100 回答