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