Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript onchange不适用于下拉菜单?_Javascript_Html - Fatal编程技术网

Javascript onchange不适用于下拉菜单?

Javascript onchange不适用于下拉菜单?,javascript,html,Javascript,Html,我正在尝试这样做,当从我的下拉菜单中进行选择时,文本将相应地显示在我的文本区域中,因为现在我一直在尝试让其中一个工作 问题:它不会在textarea内显示数组中的字符串。问题是否在此代码中 下拉菜单: <select id="dropdown" onchange="getFrames();"> <option value="1" selected="selected"> Blank </option> <option value="2"&

我正在尝试这样做,当从我的下拉菜单中进行选择时,文本将相应地显示在我的文本区域中,因为现在我一直在尝试让其中一个工作

问题:它不会在textarea内显示数组中的字符串。问题是否在此代码中

下拉菜单:

<select id="dropdown" onchange="getFrames();">
    <option value="1" selected="selected"> Blank </option>
    <option value="2"> Exercise </option>
    <option value="3"> Juggler </option>
    <option value="4"> Bike </option>
    <option value="5"> Dive </option>
</select>
然后我有这个函数

function getFrames(){
    var dropSel = getDrop.options[getDrop.selectedIndex].value;

    if(dropSel === 2){
        theStage.value = ANIMATIONS["Exercise"];
}
正在引用的数组是另一个js文件中的全局数组。

请尝试:

var theStage,getDrop; 

function getFrames() {
    var dropSel = getDrop.options[getDrop.selectedIndex].innerHTML;//+getDrop.value;
    theStage.value = ANIMATIONS[dropSel];

}

//Place the selection part on load callback
window.onload = function () {
    theStage = document.getElementById("textstage");
    getDrop = document.getElementById("dropdown");
}

  • 您可以只使用
    getDrop.value
    而不是执行
    getDrop.options[getDrop.selectedIndex].value
  • ==
    是严格的相等比较,意思是“2”==2将与您的情况一样为假
  • 看起来您正在查找选项文本,以便在对象动画中基于此作为关键点查找值。因此,您只需执行
    getDrop.options[getDrop.selectedIndex].innerHTML
  • 您的文档选择代码应该位于window.onload内或html中的元素之后

我对html做了一个小改动,省略了内联事件处理程序,而是将其添加到javascript中

html:

<select id="dropdown">
    <option value="1" selected="selected"> Blank </option>
    <option value="2"> Exercise </option>
    <option value="3"> Juggler </option>
    <option value="4"> Bike </option>
    <option value="5"> Dive </option>
</select>
<textarea id="textstage" rows="80" cols="20"> </textarea>
var theStage = document.getElementById("textstage");
var getDrop = document.getElementById("dropdown");
getDrop.addEventListener("change",getFrames);
function getFrames(){
   var dropSel = getDrop.options[getDrop.selectedIndex].value;
    if(dropSel == 2){
      theStage.value = ANIMATIONS["Exercise"];
    }
}

希望它现在能为您工作。

控件是否进入getFrames方法?我的意思是,您是否尝试在该方法中放置警报并检查事件是否被触发?当我在同一部分中实现警报时,如同(dropSel==2){当我在下拉菜单中将所选选项更改为“练习”时,不会发生任何警报。@Corjava您在控制台中看到任何错误吗?有错误。请关闭method@harsha好吧,这是一个问题,但我刚刚结束,它仍然没有工作。
<select id="dropdown">
    <option value="1" selected="selected"> Blank </option>
    <option value="2"> Exercise </option>
    <option value="3"> Juggler </option>
    <option value="4"> Bike </option>
    <option value="5"> Dive </option>
</select>
<textarea id="textstage" rows="80" cols="20"> </textarea>
var theStage = document.getElementById("textstage");
var getDrop = document.getElementById("dropdown");
getDrop.addEventListener("change",getFrames);
function getFrames(){
   var dropSel = getDrop.options[getDrop.selectedIndex].value;
    if(dropSel == 2){
      theStage.value = ANIMATIONS["Exercise"];
    }
}