以下是我所拥有的...

<html>
<body>
<form name="myForm">
<select name="myOption" multiple>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
<BR><BR>
<input type=submit value="Print First" onClick="printMe()">

<input type=submit value="Print All" onClick="printAll()">

</body>
<script>
function printMe() {
alert ("Selected option is " + myForm.myOption.value);
}

function printAll() {

var str = "";

// what should I write here??

alert("Options selected are " + str);
}


</script>
</html>

请让我知道我应该用printAll()写什么,以便可以打印所有选择的值...我知道如何打印第一个选择的值...

最佳答案

这个怎么样??

function printAll() {

var str="",i;

for (i=0;i<myForm.myOption.options.length;i++) {
    if (myForm.myOption.options[i].selected) {
        str = str + i + " ";
    }
}

alert("Options selected are " + str);
}

祝好运!!!

关于javascript - 显示多个下拉列表的所有选定值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9239523/

10-09 06:52