简体   繁体   English

文本区域的多选选项

[英]Multiple select option to a textarea

I am still learning javascript and I have problem with my current application 我仍在学习javascript,当前应用程序有问题
From this fiddle , it's not working but it can roughly show what I want to achieve. 通过这个小提琴 ,它不起作用,但是可以大致显示出我想要实现的目标。

My output if I chose Banana & Mouse will be: 如果选择Banana&Mouse,我的输出将是:
Banana is yellow 香蕉是黄色的
Mouse is small 鼠标很小

I want to know how I can call the second function with just one click of the button, and how can I reuse my codes in this case as I have another 5 more select options at the end it. 我想知道如何只需单击一下按钮就可以调用第二个函数,以及在这种情况下如何重用代码,因为最后还有另外5个选择选项。

Thanks! 谢谢!

You can just pass the ids as parameters to the function and manipulate each element value accordingly. 您可以将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;;
}

DEMO 演示

you could make it much simpler. 您可以简化它。 but here is what you were looking for a link 但是这就是您想要的链接

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