获取动态生成的选项菜单HTML的索引

时间:2016-12-01 13:20:32

标签: html

我正在努力的HTML练习是要求我动态创建某些艺术家的选项。我已经做到了这一点,但无法找到办法解决下一个问题。我被要求提醒()所选作曲家/乐队的索引值。 这似乎很简单,但我不知道该将它称为什么。

<html>
<head>
<title>Page Title</title>
<script type='text/javascript'>
classical=["Beethoven","Mozart","Tchaikovsky"];
pop=["Beatles","Corrs","Fleetwood Mac","Status Quo"];

var s="";

function show(choice){
  if(choice==1)
    music=classical;
  if(choice==2)
    music=pop;
  s="<html><head></head><body>"; //start of the line
  s+="<form id='mySelect'><select size='"+music.length+"'> "; //make length of number of artist

  for(c=0;c<music.length;c++)
    s+="<option>"+music[c]+"</option>"; //add groups as options
  s+="</select></form></body></html>";  //close line

  var line = s
  with(document.getElementById('examples').contentDocument){
    open();
    write(s);
    close();
    }

  alert(document.getElementById("mySelect").selectedIndex)
}
</script>
</head>
<body>
  <p>Dynamically written content</p>
  <form>
    <select onchange="show(this.selectedIndex);">
      <option>choose...</option>
      <option>classical</option>
      <option>pop</option>
    </select>
  </form>
  <iframe id='examples'></iframe>
</body>
</html>

感谢您的帮助!

0 个答案:

没有答案