javascript global array problem
consider a scenario
I have javascript array which is declared as global
for instance:
var globalarray=new Array();
next I have two multiple select boxes
- selectboxa
- selectboxb
all the option values inside selectbox are dynamic, if I select multiple values from both selectboxa and selectboxb. I collect the selected values in global array.
Important: if I choose values from either selectboxa or selectboxb no problem but if I select al开发者_开发技巧ternatively from selectboxa and selectboxb it creates a problem
problem: While saving I collect selected values from array, If I select 4 values from selectboxa and next 3 values from selectbox b like this alternatively I change and finally it contains all values as per global array, but I don't want like that using only one global array and get selectboxa values separately and selectbox b values separetely at same time it is possible.
If any doubts please me ask me.
I THINK I know what you want...
var globalObject = {
selecta:[],
selectb:[]
}
function setSel(sel) {
globalObject[sel.name].length=0;
for (var i=sel.selectedIndex, n=0;i<sel.options.length;i++) {
if (sel.options[i].selected) globalObject[sel.name][n++]=sel.options[i].value;
}
}
<select name="selecta" onChange="setSel(this)">
.
.
.
<select name="selectb" onChange="setSel(this)">
Perhaps this then
var globalArray = [];
function setSel(theForm) {
var sela = form.selecta;
var selb = form.selectb;
globalArray.length=0;
for (var i=0, n=0;;i++) {
if (sela.options.length<i && sela.options[i].selected) globalArray[n++]="a."+i+":"+sela.options[i].value;
if (selb.options.length<i && selb.options[i].selected) globalArray[n++]="b."+i+":"+selb.options[i].value;
}
}
<select name="selecta" onChange="setSel(this.form)">
.
.
.
<select name="selectb" onChange="setSel(this.form)">
精彩评论