在我的jQuery文件中,我具有以下脚本:
function incDistInput(){
$(".incInputBtn").on("click", function() {
var $button = $(this),
oldValue = $button.siblings("input").val(),
quantity = oldValue.split(' miles');
if ($button.hasClass('plusBtn')) {
var newVal = parseFloat(quantity[0]) + 1;
} else {
if (quantity[0] > 0) {
var newVal = parseFloat(quantity[0]) - 1;
} else {
newVal = 0;
}
}
$button.siblings("input").val(newVal + ' miles');
});
}
该函数在编译时给出以下错误:
var newVal = parseFloat(quantity[0]) - 1;
'newVal' is defined but never used. — column 17
'newVal' is already defined. — column 24
newVal = 0;
'newVal' used out of scope. — column 13
$button.siblings("input").val(newVal + ' miles');
'newVal' used out of scope. — column 39
如何在不更改函数输出的情况下重新排列或定义这些变量以清除错误?
请您参考如下方法:
尝试在newVal语句之外初始化if,以便可以识别它。
var $button = $(this),
oldValue = $button.siblings("input").val(),
quantity = oldValue.split(' miles')
var newVal = 0;
if ($button.hasClass('plusBtn')) {
newVal = parseFloat(quantity[0]) + 1;
} else {
if (quantity[0] > 0) {
newVal = parseFloat(quantity[0]) - 1;
} else {
newVal = 0;
}
}
$button.siblings("input").val(newVal + ' miles');




