简体   繁体   English

使用Java脚本将下拉列表的多个选择保存到变量中

[英]Save multiple selection of dropdown list to variables using Javascript

I have there dropdown lists with values, and one textarea to write those valuse in. When i press button it writes values of all three selected dropdown lists, it do that every time when i press the button (it do it three times). 我那里有带值的下拉列表,还有一个textarea用来写这些值。当我按下按钮时,它会写入所有三个选定的下拉列表的值,每次按下按钮时它都会这样做(它执行了3次)。 This pice of code writes values of dropodown lists, like this: Button pressed first time: "Conntent of dropdown lists" undefinedundefined Button pressed second time: undefined"Conntent of dropdown lists"undefined Button pressed third time: undefinedundefined"Conntent of dropdown lists" 此代码段将写入下拉列表的值,如下所示:第一次按下按钮:“下拉列表的内容” undefinedundefined再次按下按钮:undefined“下拉列表的内容” undefined三次按下按钮:undefinedundefined“下拉列表的内容”

But i want values of dropdown list not "undefined" 但我希望下拉列表的值不是“未定义”

What can you think of? 你能想到什么?

var i = 0;
function inc(){
i++;

if (i == 1){
var UkupnaPorudzbina1 = VrsteName + ' -> ' + PodvrsteName + ' -> ' + VelicineName + i;
}else if (i == 2){
var UkupnaPorudzbina2 = VrsteName + ' -> ' + PodvrsteName + ' -> ' + VelicineName + i;
}else if (i == 3){
var UkupnaPorudzbina3 = VrsteName + ' -> ' + PodvrsteName + ' -> ' + VelicineName + i;
}
var porudzba = UkupnaPorudzbina1 + UkupnaPorudzbina2 + UkupnaPorudzbina3;
    document.frmMain.PorudzbaHolder.value = porudzba ;
}

VrsteName is: VrsteName是:

VrsteName = document.frmMain.Vrste.options[document.frmMain.Vrste.selectedIndex].text

PodvrsteName and VelicinaName are same sort PodvrsteName和VelicinaName属于同一类

And html part is: 而html部分是:

<textarea name="PorudzbaHolder" rows="4"> </textarea>
<input type="button" value="Dodaj porudzbinu" onClick="inc();"/>

Thx in advance... 提前谢谢...

This is because you are declaring only one of the three variables each time, but then referencing all three later. 这是因为您每次仅声明三个变量之一,但随后又引用所有三个变量。 Try declaring your variables first and initializing them to "" : 尝试先声明变量,然后将其初始化为""

var UkupnaPorudzbina1 = "";
var UkupnaPorudzbina2 = "";
var UkupnaPorudzbina3 = "";

Then run the rest of your code as written, but leaving the var out before each assignment to UkupnaPorudzbina . 然后按编写的方式运行其余代码,但在每次分配给UkupnaPorudzbina之前将var排除UkupnaPorudzbina

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

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