Article Detail

AngularJS购物车商品数量加减价格实时变化代码

价格计算器 数量: - 加 价格: 总价: {{quantity * price}} var app = angular.module('myApp', [])...

HTML 阅读 48.8万 1 分钟阅读 2016-03-25 12:02
GDScript3
30 行
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> 
</head>
<body>

<div ng-app="myApp" ng-controller="personCtrl" data-ng-app="" data-ng-init="quantity=1;price=5">

<h2>价格计算器</h2>

数量:<button ng-click="toggle();" >-</button> <input type="number" ng-change="toggles()" ng-model="quantity"><button ng-click="quantity = quantity + 1">加</button>
价格: <input type="number" ng-model="price">

<p><b>总价:</b> {{quantity * price}}</p>

</div><script>
	var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.toggle = function() {
      if($scope.quantity>1) $scope.quantity =$scope.quantity - 1;
    };
	$scope.toggles = function() {
      if($scope.quantity<0 || isNaN($scope.quantity)) $scope.quantity =1;
    };
	});
	</script>
</body>
</html>
Comments 评论区
广西南宁市 2016-03-25 12:03

js有个函数 isNaN(val)//如果是数字则返回 false

使用时候

代码片段
5 行
if(!isNaN(val)){
   alert("是数字");
}else{
   alert("不是数字");
}
| | #0
Comment Form 留下评论
正在回复 #0
粘贴图片、拖拽文件,或点上面的按钮上传 图片会自动插入 [img] 标签,其他附件会自动插入 [attach] 标签。
正在上传...
提交前会先拉起旧项目同款第三方人机验证。

不再要求填写昵称;reply 会生成一条带楼层回链的新评论,quote 会附带完整引用块,并保持评论锚点跳转。

lizhenqiu blog is powered by lizhenqiu.com Version 6.9

Processed in 0.0076 second(s) W3C

本博客的所有原创作品采用 知识共享 署名-非商业性使用-相同方式共享 2.5 协议 进行许可

本站由 七七牛 云存储 阿阿里云 计算与安全服务 拍又拍云 CDN 加速 百百度智能 AAMH 布布集网 AI指南针AI

桂公网安备 45010302000998号 桂ICP备15007619号-1 中国互联网举报中心 建议使用谷歌浏览器浏览
Navigation 文章导航
⌂ ↓ ↑
100%
图片预览
Gallery 图集
0 张图片

正在整理正文和评论里的图片。

当前页还没有可展示的图片。
Quick Comment 快速评论

直接输入内容,提交时仍会走当前页面的人机验证。

正在上传...
操作提示