【发布时间】:2013-07-19 13:07:51
【问题描述】:
我有以下 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>
我想修改上述函数,以便能够在不知道值的情况下从下拉列表中选择“另一个”...
有什么建议吗?
提前致谢,
Zsolt
【问题讨论】:
-
你的意思是你想通过索引选择
<option>,或者通过它的文本? -
这里有
select <option> by text、select <option> by value,如果你简化后者,你会得到索引选择。 -
我想根据文字选择
-
那么您可能正在寻找
code like this。我已经使用tagName属性改进了这个非常不安全的部分。 -
感谢 TLama,再次需要这个,并找到了您的代码提示 :)
标签: html delphi twebbrowser