将 .change 函数中变量的值传递和更新到全局变量 Jquery
Pass and update values from variable in .change function to global variable Jquery
我正在寻找一种方法将值数组从 .change(function{})
中的 var selectedId = []
传递到全局 var globalId = []
。所以每次selectedId
中的值发生变化时,我都希望有一个像selectedId.update(globalId)
或selectedId = globalId.update()
这样的函数来更新全局id,但我不知道这部分怎么写.最终,我可以在其他更改功能中使用最新的 globalId
。这是我的代码
$(function(){
var globalId = [];
$("#listbox1").change(function(){
var selectedId =[];
//get each selection
$.each(selectedId , function (fIndex, fValue) {
$.each(this.options, function (value, option) {
if (this.selected) {
selectedId.push(option.value);
}
});
});
// send value to globalId
selectedId = globalId; // how to keep globalId up-to-date
// for everytime selectedId changes
$.ajax({
//....
traditional: true,
data:{"// id in the controller": selectedId},
success{...},
error{...}
});//end ajax
});//end listbox1 change
$("#listbox2").change(function(){
var selectedId2 = [];
// same each function to get selected <option>s as listbox1
$.each{...
$each{...
};
};
$.ajax({
//...
data:{
"//id1 in the controller": selectedId2,
"//id2 in the controller": globalId
},// this is the reason why I want to pass those values from listbox1
//to globalId so that I can use these two arrays in my controller function
success{...},
error{}
});
})//end listbox2 change
});//end ready
如果有人有解决这个问题的想法,我将不胜感激。
谢谢!
凯文
你可以这样做。
function GlobalIds(){
var globalIds;
return{
getGlobalIds: function(){ return globalIds;},
setGlobalIds: function(ids){ globalIds = ids}
}
}
var globalIds = new GlobalIds();
globalIds.setGlobalIds([1,2]);
console.log(globalIds.getGlobalIds());
globalIds.setGlobalIds([3,4]);
console.log(globalIds.getGlobalIds());
而且你好像做错了作业。
而不是
// send value to globalId
selectedId = globalId; // how to keep globalId up-to-date
// for everytime selectedId changes
应该是
globalId = selectedId;
我正在寻找一种方法将值数组从 .change(function{})
中的 var selectedId = []
传递到全局 var globalId = []
。所以每次selectedId
中的值发生变化时,我都希望有一个像selectedId.update(globalId)
或selectedId = globalId.update()
这样的函数来更新全局id,但我不知道这部分怎么写.最终,我可以在其他更改功能中使用最新的 globalId
。这是我的代码
$(function(){
var globalId = [];
$("#listbox1").change(function(){
var selectedId =[];
//get each selection
$.each(selectedId , function (fIndex, fValue) {
$.each(this.options, function (value, option) {
if (this.selected) {
selectedId.push(option.value);
}
});
});
// send value to globalId
selectedId = globalId; // how to keep globalId up-to-date
// for everytime selectedId changes
$.ajax({
//....
traditional: true,
data:{"// id in the controller": selectedId},
success{...},
error{...}
});//end ajax
});//end listbox1 change
$("#listbox2").change(function(){
var selectedId2 = [];
// same each function to get selected <option>s as listbox1
$.each{...
$each{...
};
};
$.ajax({
//...
data:{
"//id1 in the controller": selectedId2,
"//id2 in the controller": globalId
},// this is the reason why I want to pass those values from listbox1
//to globalId so that I can use these two arrays in my controller function
success{...},
error{}
});
})//end listbox2 change
});//end ready
如果有人有解决这个问题的想法,我将不胜感激。
谢谢!
凯文
你可以这样做。
function GlobalIds(){
var globalIds;
return{
getGlobalIds: function(){ return globalIds;},
setGlobalIds: function(ids){ globalIds = ids}
}
}
var globalIds = new GlobalIds();
globalIds.setGlobalIds([1,2]);
console.log(globalIds.getGlobalIds());
globalIds.setGlobalIds([3,4]);
console.log(globalIds.getGlobalIds());
而且你好像做错了作业。 而不是
// send value to globalId
selectedId = globalId; // how to keep globalId up-to-date
// for everytime selectedId changes
应该是
globalId = selectedId;