我有以下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>
我想修改上面的功能,以便能够从下拉菜单中选择“另一个&#39;不知道价值......
有什么建议吗?
提前致谢,
泽索特
答案 0 :(得分:0)
能够在不知道值的情况下从下拉列表中选择“另一个”
那么通过索引?选项编号2(从零开始)?
让我们从searching for data type in Google
开始第一个链接是MSDN specifications for that datatype in Microsoft Internet Explorer。在右侧,您可以看到selectedIndex: Integer属性。
所以代码就像
else if field.tagName = 'SELECT' then
begin
selectField := field as IHTMLSelectElement;
selectField.selectedIndex := 2;
end
回到谷歌,第三个链接就是使用该属性的source code example。
答案 1 :(得分:0)
selectField.value := (selectField.item(selectField.length-1,EmptyParam) as IHTMLOptionElement).value;