gpt4 book ai didi

javascript - 添加文本框的数字会给出错误的结果

转载 作者:行者123 更新时间:2023-12-03 05:28:03 24 4
gpt4 key购买 nike

我正在创建一个网络应用程序,其中的 $scope 变量中有一个数字

$scope.total=20;

我有一个文本框

<input type="text" ng-model="amount1" ng-change="amountchange(amount1)" />

当用户在文本框中输入 500 时,它应该是 700,但答案却是 755,当我从文本框中删除 500 时,它变成了 null

我只想从文本框中添加或删除我在这里做错的事情

这是一个fiddle我为了更好地理解而创建的

最佳答案

这个问题在这一行:

$scope.total = parseInt($scope.amount1) + parseInt($scope.total);

当您输入更多输入时,您会聚合值,而不是仅仅播种结果。

你能做的很简单,添加这个方法:

 $scope.getTotal=function(){
return parseInt($scope.amount1) + parseInt($scope.total);
}

并将 {{total}} 替换为:{{getTotal()}}

关于javascript - 添加文本框的数字会给出错误的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41075243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com