[英]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.