简体   繁体   English

如何单击下拉菜单并选择选项?

[英]How to click a dropdown menu and select option?

I have the following code 我有以下代码

<select class="needsclick" id="country" name="order[country]">
              <option value="USA" selected="selected">USA</option>
              <option value="CANADA">CANADA</option>
            </select>

I could do the following javascript command to change the option value 我可以执行以下javascript命令来更改选项值

document.getElementById("country").value = 'CANADA'

however, this does not change the selected value and does not change the state box to province. 但是,这不会更改所选值,也不会将状态框更改为省。

When I physically click this dropdown menu and change to CANADA, the effects of the change take place (the state box changes to province) 当我实际单击此下拉菜单并更改为CANADA时,更改的效果就会发生(状态框更改为Province)

I am using Swift iOS to parse HTML and wondering what line of javascript code is needed to click a option value rather than changing the option value? 我正在使用Swift iOS解析HTML,并想知道单击选项值而不是更改选项值需要一行JavaScript代码?

If I do the following after changing the value 如果更改值后执行以下操作

document.getElementById("country").click()

it just clicks the menu but still does not change the state box to province (happens when physically clicking the option value) 它仅单击菜单,但仍不会将状态框更改为省(在物理上单击选项值时发生)

How can I achieve this using a javascript command like the two above? 如何使用上述两个的javascript命令实现此目标?

The state/province box is irrelevant to the code just relevant to the fact it changes when the dropdown is physically clicked and not when programmatically changed. 状态/省份框与代码无关,只是与实际单击下拉列表而不是通过编程更改而更改的事实相关。

I can do the following code but it still does not change the state box to province (only if physically clicked) 我可以执行以下代码,但是它仍然不会将状态框更改为省(仅在物理单击的情况下)

document.getElementById("country")[1].selected = true

To work with the option elements within a select , you must access the options node list and then select one to work with. 要使用select内的option元素,必须访问options节点列表,然后选择一个要使用的元素。

Setting the value is separate from setting the selected flag. 设置值与设置selected标志是分开的。

 var countries = document.getElementById("country"); var states = document.getElementById("provincesUSA"); var territories = document.getElementById("provincesCanada"); countries.addEventListener("change", function(e) { update(e); }); function update(e){ // show the correct sub-list based on the selected option var country = countries[countries.selectedIndex]; if(country.value === "USA"){ states.classList.remove("hidden"); territories.classList.add("hidden"); } else if(country.value === "CANADA") { territories.classList.remove("hidden"); states.classList.add("hidden"); } else { territories.classList.add("hidden"); states.classList.add("hidden"); } } // To dynamically choose document.querySelector("button").addEventListener("click", function(){ countries.options[2].selected = "selected"; // Canada // Simulate the change event update(countries); }); 
 #provincesUSA.hidden, #provincesCanada.hidden { display:none; } 
 <select class="needsclick" id="country" name="order[country]"> <option value="" selected="selected">Choose A Country</option> <option value="USA">USA</option> <option value="CANADA">CANADA</option> </select> <select id="provincesUSA" class="hidden" name="states"> <option value="al">Alabama</option> <option value="ar">Arkansas</option> </select> <select id="provincesCanada" class="hidden" name="territories"> <option value="on">Ontario</option> <option value="qu">Quebec</option> </select> <button>Force A Selection (click me whe CANADA is NOT selected)</button> 

document.getElementById("country").selectedIndex = 2;

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

相关问题 如何添加一个下拉菜单,当您 select 一个选项并单击发送时,它会显示 email 中的选项? - How do I add a dropdown menu that when you select an option and click send it shows the option in the email? 如何在下拉菜单中动态 select 选项? - How to dynamically select option in dropdown menu? 单击角度选择选项下拉菜单中的按钮时如何选择对象的多个值 - How to select multitple value of object when click on button in Angular selection option dropdown menu Jquery / Javascript如何根据下拉菜单中的select选项更改按钮单击function? - Jquery/Javascript how to change button click function based on select option in dropdown menu? 使用javascript选择下拉菜单选项 - Select dropdown menu option with javascript 如何在同一选项/下拉菜单中的选择中扩展选项 - How to expand options in a select in the same option/dropdown menu 如何在 HTML/纯 JS 中保存 select 下拉菜单选项? - How to save select dropdown menu option in HTML/pure JS? 如何在 Vue 3 的下拉菜单中自动选择第一个选项 - How to automatically select the first option in a dropdown menu in Vue 3 如何使用 select 选项制作多级下拉菜单 - How to make multi level dropdown menu using select option mentod 如何单击与硒和python不可交互的下拉菜单,然后选择一个选项? - How to click not interactable dropdown with selenium and python and select one option?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM