我尝试使用this.calculatePrice
,但不起作用
并且无法从内部访问外部变量minTraveller
numberSpin(min: number, max: number) {
$(document).on('click', '.number-spinner button', function () {
const btn = $(this);
// let oldValue = 0;
let newValue = 0;
const oldValue = Number(btn.closest('.number-spinner').find('input').val());
if (btn.attr('data-dir') == 'up') {
if (oldValue < max) {
newValue = oldValue + 1;
} else {
newValue = max;
}
} else {
if (oldValue > min) {
newValue = oldValue - 1;
} else {
newValue = min;
}
}
btn.closest('.number-spinner').find('input').val(newValue);
sessionStorage.setItem('totalAdult', String(newValue));
calculatePrice();
});
// console.log(this.minTraveler);
}
calculatePrice() {
console.log(sessionStorage.getItem('totalAdult'));
}
我该如何解决?
答案 0 :(得分:0)
您可以这样做,这是我的工作。
numberSpin(min: number, max: number) {
$(document).on('click', '.number-spinner button', () => this.handleClick());
}
handleClick() {
const btn = $(this);
// let oldValue = 0;
let newValue = 0;
const oldValue = Number(btn.closest('.number-spinner').find('input').val());
if (btn.attr('data-dir') == 'up') {
if (oldValue < max) {
newValue = oldValue + 1;
} else {
newValue = max;
}
} else {
if (oldValue > min) {
newValue = oldValue - 1;
} else {
newValue = min;
}
}
btn.closest('.number-spinner').find('input').val(newValue);
sessionStorage.setItem('totalAdult', String(newValue));
this.calculatePrice();
}
calculatePrice() {
console.log(sessionStorage.getItem('totalAdult'));
}