Javascritptのみを使用して複数選択から値を取得(または表示)しようとしています。ユーザーはここから複数のオプションを選択でき、「表示選択」ボタンをクリックすると、これらのオプションの値がわかります。 私は 'selected'属性について考えましたhere しかし、コードは機能しませんでした。どんな助け?Javascript Show複数選択オプション値
<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