繁体   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