我試圖從僅使用Javascritpt的Multiple Select中獲取(或顯示)值。比方說,用戶可以從這裏選擇多個選項,當他們點擊「顯示選擇」按鈕時,他們可以看到這些選項的值。 我從here 的想法'選擇'屬性但是,代碼沒有工作。任何幫助?Javascript顯示多個選擇選項值
<select id ="selectOptions" name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<button onclick="myFunction()">Show Selects</button>
<script>
function myFunction()
{
var numOfOptions = document.getElementById("slectOptions").options.length;
var n=0;
for (n=0; n<numOfOptions; n++)
{
// I tried to use a for loop that will go around from option 0 to 3,
//and through 'selected' attribute wanted to check the condition if the option is selected then only show the values
// I still couldn't figure out if the loop is working at all
if (document.getElementById("slectOptions")[n].selected)
{
var x = document.getElementById("slectOptions").value;
window.alert(x);}
}
}
的選擇的ID是'selectOptions'但你通過'slectOptions'多次提到它 – j08691