从onChange函数中存储全局变量中的值可以通过以下几种方式实现:
var globalVariable = "";
function onChange(event) {
globalVariable = event.target.value;
}
function createClosure() {
var globalVariable = "";
return function onChange(event) {
globalVariable = event.target.value;
}
}
var onChangeClosure = createClosure();
var eventManager = {
subscribers: {},
subscribe: function(event, callback) {
if (!this.subscribers[event]) {
this.subscribers[event] = [];
}
this.subscribers[event].push(callback);
},
publish: function(event, data) {
if (this.subscribers[event]) {
this.subscribers[event].forEach(function(callback) {
callback(data);
});
}
}
};
var globalVariable = "";
function onChange(event) {
globalVariable = event.target.value;
eventManager.publish('valueChanged', globalVariable);
}
eventManager.subscribe('valueChanged', function(value) {
// Do something with the value
});
无论使用哪种方式,都可以在onChange函数中获取到输入框的值,并将其存储到全局变量中供其他地方使用。
领取专属 10元无门槛券
手把手带您无忧上云