[英]Assigning JavaScript variables with HTML drop-down menu (user selection)
[英]Javascript selectedIndex is returning undefined on a HTML drop-down menu
目前,我正在尝试编写一种将下拉菜单的值附加到html链接的方法。 但是,我很难从菜单中检索选择的文本。 我将问题缩小为Javascript中的selectedIndex方法。 返回未定义。 我在下面包括了我的方法。
function getSize(productID){
var sizeBox = document.getElementsByName(productID);
alert(document.getElementsByName(productID).selectedIndex);
var sizeSelected = sizeBox.options[sizeBox.selectedIndex].text;
alert(sizeSelected);
var link = document.getElementById(productID).getAttribute("href");
link = link + "&size=" + sizeSelected;
document.getElementById(productID).setAttribute("href",link);
return true;
}
getElementsByName
返回一个HTMLCollection 。 您应该说: getElementsByName[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.