![](/img/trans.png)
[英]How to change a text box to visible depending on what item is selected in a drop down menu?
[英]How to programmatically change the selected item in this drop down menu?
<DIV class="uiMenu uiSelectorMenu" role=menu>
<UL class=uiMenuInner bindPoint="menu">
<LI class="uiMenuItem uiMenuItemRadio uiSelectorOption checked" data-label="Paused"><A aria-checked=true class=itemAnchor role=menuitemradio tabIndex=0 href="#" rel=ignore><SPAN class="itemLabel fsm"><SPAN class=icon_wrap><IMG class="selector icon" src="/images/adz/ad_states/paused.gif"></SPAN>Paused</SPAN></A></LI>
<LI class="uiMenuItem uiMenuItemRadio uiSelectorOption " data-label="Active"><A aria-checked=true class=itemAnchor role=menuitemradio tabIndex=1 href="#" rel=ignore><SPAN class="itemLabel fsm"><SPAN class=icon_wrap><IMG class="selector icon" src="/images/adz/ad_states/running.gif"></SPAN>Active</SPAN></A></LI>
<LI class="uiMenuItem uiMenuItemRadio uiSelectorOption " data-label="Deleted"><A aria-checked=true class=itemAnchor role=menuitemradio tabIndex=2 href="#" rel=ignore><SPAN class="itemLabel fsm"><SPAN class=icon_wrap><IMG class="selector icon" src="/images/adz/ad_states/deleted.gif"></SPAN>Deleted</SPAN></A></LI></UL></DIV></DIV><SPAN bindPoint="copy_link_wrapper"><A class=copy_link href="#"></A></SPAN></DIV><SELECT bindPoint="select"><OPTION value=""></OPTION><OPTION selected value=2>Paused</OPTION><OPTION value=1>Active</OPTION><OPTION value=99>Deleted</OPTION></SELECT> </DIV></TD>
<TD id=td_time_start_html_last_row class=td_time_start_html ?>
以上是我想要更改的下拉菜单的HTML代码。
在代码的最后它有SELECT,OPTION等..我通常可以改变一些东西,但是当我更改selectedIndex它在网站上没有做任何事情时,我假设代码的开头部分有一些东西给做到这一点。
这是我的VB6代码(注意你可以用任何语言显示我)
For i = 0 To Form1.WebBrowser1.Document.All.length - 1 '
If Form1.WebBrowser1.Document.All.Item(i).nodeName = "SELECT" Then
For x = 0 To Form1.WebBrowser1.Document.All.Item(i).Options.length - 1
If InStr(Form1.WebBrowser1.Document.All.Item(i).Options(x).Text, "Active") > 0 Then
Form1.WebBrowser1.Document.All.Item(i).selectedIndex = x
End If
Next x
End If
Next i
当我运行该代码时,我使用Debug.Print Form1.WebBrowser1.Document.All.Item(i).Options(x)检查items值。选择它确实返回true,而之前选择的确实是false,但是网站该项目不会更改/更新。
任何帮助是极大的赞赏。
使用选项的.selected属性; 这将自动更新选择的.selectedIndex。 POC .HTA:
<html>
<head>
<Title>select</Title>
<hta:application id="select" scroll = "no">
<script type="text/vbscript">
Function doIt()
' select Item2
With document.getElementById("lbDemo")
MsgBox "Before: " & .selectedIndex
.options(1).selected = True
MsgBox "After: " & .selectedIndex
End With
End Function
</script>
</head>
<body>
<select id="lbDemo" size="4">
<option value=Item1>Item1</option>
<option value=Item2>Item2</option>
<option value=Item3>Item3</option>
</select>
<hr />
<input type="button" value="select 2" onclick="doIt" />
</body>
</html>
当我在VisualBasic 2012 Win-Forms应用程序中运行示例时,不确定这会有多大帮助,
Dim selectList = WebBrowser1.Document.GetElementsByTagName("select").Item(0)
selectList.SetAttribute("selectedIndex", 2)
但是使用HTML片段作为Web浏览器控件的页面内容,上面的代码仅使用HTMLElement类正确选择了下拉列表"Active"
的第二项。 屏幕截图如下:
如果您可以在某个地方发布完整的源代码,那么可能更容易找出您的具体问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.