簡體   English   中英

如何使用JavaScript選擇下拉菜單選項

[英]How to select a dropdown option using JavaScript

我正在編程在此網站上選擇10.5尺寸的東西: http : //www.ruvilla.com/new/men/footwear/nike-air-foamposite-one-concord.html

目前,我正在使用Google Chrome控制台測試代碼。 這是我的代碼:

document.getElementsByClassName('villa-lb-span').getAttribute = "10.5".select;

Google Chrome控制台中的結果為“未定義”。 我的目標是選擇大小10.5。有人可以向我提供一些有關我的代碼的反饋嗎?

尺寸存儲在下拉列表中。 我只是不確定為什么我的代碼無法正常工作。 我已經為此工作了大約2個小時,並且已經檢查了該論壇的所有內容。 似乎對我沒有任何幫助。 任何幫助將不勝感激。 謝謝!

非常簡單地將下拉節點的value屬性設置為您想要將其更改為的值。

document.getElementById('attribute196').value = 184;//since that is the value of the 10.5 option

使用純JavaScript,您需要做一些工作。 遍歷選項,然后選擇值:

var options = document.getElementById('attribute196').options; //Get all the options of the dropdown
for (var i = 0; i < options.length; i++){
    if (options[i].value == "184") { //The '10.5' option has a value of "184" 
        options[i].selected = true;
        break;
    }
}
document.querySelector('.villa-lb-span').options[document.querySelector('.villa-lb-span').selectedIndex].value;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM