gpt4 book ai didi

javascript - 如果文本框为空,则出现 angularjs Nan

转载 作者:行者123 更新时间:2023-11-30 11:45:29 24 4
gpt4 key购买 nike

我正在创建一个网络应用程序,我在其中计算一个带有 textbox$scope 变量,但是如果用户将 textbox 设为空或者如果用户没有在 textbox 上输入任何内容,结果将是 NaN 这是我的代码

<input type="text" ng-model="amount1" ng-blur="amountchange(amount1)" />
{{total}}

在我的 Controller

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

我想去掉文本框为空时出现的 NaN

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

CLICK HERE

最佳答案

使用

$scope.constant = 200;     //Define other variable to persist constants
$scope.total = $scope.constant; //Initialize total
$scope.amountchange = function() {
var amount1 = parseInt($scope.amount1, 10) || 0; //parse amount if its invalid set it to zero
$scope.total = $scope.constant + amount1; //update total
}

DEMO

关于javascript - 如果文本框为空,则出现 angularjs Nan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41114678/

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