【问题标题】:How can I make a decision according to the value of selectedIndex如何根据 selectedIndex 的值做出决定
【发布时间】:2013-12-16 06:34:56
【问题描述】:

我想制作一个音频 HTML 标签,以根据选择标签中选择的选项更改其来源,我该怎么做?

我正在考虑 index.html 文件中的类似内容:

<script src="hola.js"></script>
<form name="datos">
    <select id="idioma">
        <option value="es">Español</option>
        <option value="en">Inglés</option>
    </select>
    <br />
    <button id="boton">Cambiar idioma</button>

</form>
<audio controls="controls" id="audio">Audio no disponible, para mejor funcionamiento, utilize Mozilla Firefox</audio>   

这在 hola.js 文件中:

var button=document.getElementById("boton");
var idioma=document.getElementById("idioma");
var audio=document.getElementById("audio");
var lang=document.datos.idioma.options[idioma.selectedIndex].value;

button.addEventListener("click",function(){

if(lang="es"){
    audio.setAttribute("src", "audio/es.ogg");
}else if(lang="en"){
    audio.setAttribute("src", "audio/en.ogg");
}
});

【问题讨论】:

  • 这是什么?hola.js
  • @dholakiyaankit hola.js 是一个用于使按钮工作的 javascript 文件,我希望该按钮使 中的选定选项从不同的文件中获取源>

标签: javascript html html5-audio html5-apps


【解决方案1】:

在事件监听器中移动 lang 变量声明:

var button = document.getElementById("boton"),
    idioma = document.getElementById("idioma"),
    audio = document.getElementById("audio");

document.getElementsByTagName('body')[0].appendChild(audio);

button.addEventListener("click", function () {
    var lang = document.datos.idioma.options[idioma.selectedIndex].value;

    if (lang = "es") {
        audio.setAttribute("src", "audio/es.ogg");
        audio.play();
    } else if (lang = "en") {
        audio.setAttribute("src", "audio/en.ogg");
        audio.play();
    }
});

【讨论】:

  • 添加了附加到body和audio.play(),再试一次
猜你喜欢
  • 2011-07-22
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多