我试图从仅使用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