我有一个数组 betSlip
,我正在其中进行一些验证,假设该数组现在有 4 个索引,每个索引都有一个 input
,所以我有 4 个输入,每个索引一个。
示例:我在输入中键入的值:
1 input: 10
2 input: 10
3 input: 10
4 input: 10
下一个输入不能大于前一个。 这在我的应用程序中意味着如果我转到第二个输入并键入数字 1,输入 3 和 4 应该再次返回到 0。但正如我目前在我的应用程序中使用的那样,如果您在第二个输入中键入 1,则只有第三个输入返回 0 而不是其他输入。
所以,要明确一点:如果我有 10 个输入,我在每个输入中都输入了数字 100,然后转到第 3 个输入并输入 99,那么其余的输入应该恢复为 0。除了以前的输入。
我正在使用 Angular 和 Lodash。
拍了几张照片
在这张图片中,我在第二个输入中输入了 1,只有第三个输入为 0,我需要每个输入都返回 0,与当前索引分开。
这是我的代码:
$scope.riskWinCalculations2 = function(slip, type, index) {
var previousSlip = $scope.betSlip[index - 1],
currentSlip = $scope.betSlip[index],
nextSlip = $scope.betSlip[index + 1];
if (currentSlip.risk.length && nextSlip != undefined) {
if (currentSlip.risk < nextSlip.risk) {
nextSlip.risk = 0;
nextSlip.win = 0;
}
}
}
我知道我的错误出在这个 var nextSlip
中,我在这里所做的就是告诉应用程序在下一个输入中放入一个空字符串(如果当前输入的数量较少)。但这就是我来这里的原因,我需要知道如何解决它,同时考虑所有接下来的输入,而不仅仅是正确的下一个输入。
更新以添加更多说明
如果每个输入的值都是 10,而你有 5 个输入,则意味着:
1 input: 10
2 input: 10
3 input: 10
4 input: 10
5 input: 10
如果你转到第 3 个输入并键入 5,则表示:
1 input: 10
2 input: 10
3 input: 5
4 input: 0
5 input: 0
因此,如您所见,其他输入返回到初始值 0。但之前的输入保持不变。
最佳答案
//if the user tries to play with the input:
//he can not put lower amount in a previous input,
//when the next input is already filled out
_.each($scope.betSlip, function(slip, indexSlip) {
if (indexSlip > index) {
slip.risk = '';
slip.win = '';
}
});
关于javascript - 清除与索引分开的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31076262/