[英]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.