简体   繁体   English

尝试 select 下拉列表中的值

[英]Trying to select a value from a drop-down

One Form on the page I am accessing.我正在访问的页面上的一个表格。 This form has a dropdown with 3 options.此表单有一个包含 3 个选项的下拉列表。 I need to select the third option.我需要 select 第三个选项。 Accessing the webpage from a C# program with cefsharp library (embedded web browser).从带有 cefsharp 库(嵌入式 web 浏览器)的 C# 程序访问网页。

I can change the displayed text using我可以使用更改显示的文本

document.forms[0].getElementsByClassName('css-0 Select__single-value')[0].innerText="Keep in-play";

but this has not been registered.但这还没有注册。 Have also tried to trigger input and change events but it is still not recognising my selection.还尝试触发输入和更改事件,但它仍然无法识别我的选择。 Any suggestions?有什么建议么?

Here is the original DOM for the form:这是表单的原始 DOM:

 <div class="bet-widget-optional-params"> <div class="bet-params"> <div class="param-wrapper"><span class="param-label"><a class="link" href="https://help.smarkets.com/hc/en-gb/articles/115003946591" target="_blank">Time In Force</a></span> <div class="param-select"> <div class="css-0 Select custom-select-box Select menu-on-click"> <div class="css-0 Select__control"> <div class="css-0 Select__value-container Select__value-container--has-value"> <div class="css-0 Select__single-value">Default</div><input id="react-select-bet-param-selector-input" readonly="" tabindex="0" class="css-14uuagi" value=""></div> <div class="css-0 Select__indicators"><span class="css-0 Select__indicator-separator"></span><span aria-hidden="true" class="Select__arrow-zone"><span class="Select__arrow"></span></span> </div> </div> </div> </div> </div> </div> </div>

In JavaScript you can set the selectedIndex property.在 JavaScript 中,您可以设置selectedIndex属性。

elem.selectedIndex = 2;

Setting -1 will remove any selection, and any positive integer will select that item in the list (0-indexed)... so if you want the 3rd item, just set it to 2 .设置-1将删除任何选择,任何正数 integer 将 select 列表中的该项目(0索引)......所以如果你想要第三个项目,只需将其设置为2

HTMLSelectElement.selectedIndex docs HTMLSelectElement.selectedIndex 文档

The property works as a getter too, so if you want to know which element is selected, you can request it like:该属性也可用作吸气剂,因此如果您想知道选择了哪个元素,可以像这样请求它:

var currentSelectedIndex = elem.selectedIndex;

Full interactive example below:下面的完整交互示例:

 var selectElem = document.getElementById('select'); var pElem = document.getElementById('p'); var buttonElem = document.getElementById('button'); selectElem.addEventListener('change', function() { var index = selectElem.selectedIndex; pElem.innerHTML = 'selectedIndex: ' + index; }); buttonElem.addEventListener('click', function(){ selectElem.selectedIndex = 2; });
 <p id="p">selectedIndex: 0</p> <select id="select"> <option selected>Option A</option> <option>Option B</option> <option>Option C</option> <option>Option D</option> <option>Option E</option> </select> <input type="button" id="button" value="Set it to C (2)"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM