繁体   English   中英

文本区域的多选选项

[英]Multiple select option to a textarea

我仍在学习javascript,当前应用程序有问题
通过这个小提琴 ,它不起作用,但是可以大致显示出我想要实现的目标。

如果选择Banana&Mouse,我的输出将是:
香蕉是黄色的
鼠标很小

我想知道如何只需单击一下按钮就可以调用第二个函数,以及在这种情况下如何重用代码,因为最后还有另外5个选择选项。

谢谢!

您可以将id作为参数传递给函数,并相应地操作每个元素值。

function displayResult (fruitId,sizeId) {
    var selTag = document.getElementById(fruitId);
    var fruit=selTag.options[selTag.selectedIndex].text;
    if (fruit=="Apple") {
      fruit = fruit + " is red";
    } else if (fruit=="Orange") {
      fruit = fruit + " is orange";
    } else if (fruit=="Banana") {
      fruit = fruit + " is yellow";
    }
    selTag = document.getElementById(sizeId);
    var animal=selTag.options[selTag.selectedIndex].text;
    if (animal=="Elephant") {
      animal = animal + " is big";
    } else if (animal=="Mouse") {
      animal = animal + " is small";
    } else if (animal=="Whale") {
      animal = animal + " is enormous";
    }
    document.getElementById('mytextbox').value = fruit+"\n"+animal;;
}

演示

您可以简化它。 但是这就是您想要的链接

function displayResult (selId) {
    var selTag = document.getElementById(selId);
    var fruit=selTag.options[selTag.selectedIndex].text;
if (fruit=="Apple") {
  fruit = fruit + " is red";
} else if (fruit=="Orange") {
  fruit = fruit + " is orange";
} else if (fruit=="Banana") {
  fruit = fruit + " is yellow";
}
  document.getElementById('mytextbox').value = fruit;
  animalText();
}

function animalText(){
  var selTag = document.getElementById("size");
    var _size=selTag.options[selTag.selectedIndex].text;

  if (_size=="Elephant") {
  _size = _size + " is big";
} else if (_size=="Mouse") {
  _size = _size + " is small";
} else if (_size=="Whale") {
  _size = _size + " is enormous";
}

      document.getElementById('mytextbox').value = document.getElementById('mytextbox').value +"\n"+ _size;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM