在我的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
如何在不更改功能输出的情况下重新排列或以其他方式定义这些变量以清除错误?
答案 0 :(得分:1)
尝试初始化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');