<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数字输入框</title>
<script type="text/javascript" src="js/lib/vue.js"></script>
<script type="text/javascript" src="js/input-test-num.js"></script>
</head>
<body>
<div id="app">
{{value}}
<number-input v-model="value" :max="10" :min="-10" :step="2"></number-input>
</div>
</body>
<script type="text/javascript">
function isValueNumber(value){
return (/(^-?[0-9]+\.{1}\d+$)|(^-?[1-9][0-9]*$)|(^-?0{1}$)/).test(value);
} var app = new Vue({
el:'#app',
data:{
value:0
}
});
</script>
</html>

  JS:input-test-num.js

Vue.component('number-input',{
// 定义来自父级的参数
props:{
max:{
type:Number,
default:Infinity
},
min:{
type:Number,
default:-Infinity
},
value:{
type:Number,
default:0
},
step:{
type:Number,
default:1
}
},
template:'\
<div>\
<input type="text" :value="currentValue" @change="handleChange" @keydown="handleKeydown" />\
<button @click="handleAdd" :disabled="currentValue>=max"> ADD </button>\
<button @click="handleReduce" :disabled="currentValue<=min"> Reduce </button>\
</div>',
data:function(){
// Vue组件是单向数据流,无法直接修改prop中的值
var currentValue = 0 ;
if(this.value > this.max){
currentValue = this.max ;
} else if(this.value < this.min){
currentValue = this.min ;
} else{
currentValue = this.value ;
}
return {
currentValue :currentValue
}
},
methods:{
handleChange:function(event){
var val = event.target.value.trim() ;
if(isValueNumber(val)){
var max = this.max ;
var min = this.min ;
val = Number(val);
this.currentValue = val ;
if(val>max) this.currentValue = max ;
if(val<min) this.currentValue = min ;
}else{
// 如果输入的非数字,则保留之前的数据
event.target.value = this.currentValue ;
}
},
// 绑定键盘事件
handleKeydown:function(event){
if(event.keyCode==38){
this.handleAdd();
}
if(event.keyCode==40){
this.handleReduce() ;
}
},
handleAdd:function(){
if(this.currentValue>=this.max) return ;
this.currentValue += this.step ;
},handleReduce:function(){
if(this.currentValue<=this.min) return ;
this.currentValue -= this.step;
},
updateVal:function(val){
if(val>this.max) val = this.max ;
if(val<this.min) val = this.min ;
this.currentValue = val ;
}
},
watch:{
// 增加监听
currentValue:function(val){
this.$emit('input',val);
this.$emit('on-change',val);
},
value:function(val){
this.updateVal(val);
}
}
})

!可根据自己情况增加样式

最新文章

  1. 学习Find函数和select
  2. javaWeb之maven多数据库环境的配置信息
  3. JavaScript中正则表达式test()、exec()、match() 方法区别
  4. ios开发-载入viewcontroller的几种方式
  5. Linux 统计某个字符串出现的次数
  6. 鸟哥的linux私房菜学习记录之程序管理和SElinux初探
  7. WinForm 中两个窗口之间传递数据
  8. Java读取Properties配置文件
  9. THREE.js代码备份——canvas_lines(随机点、画线)
  10. 如何用chrome修改js代码,跳过网站等待时间
  11. Android中的FrameLayout帧布局
  12. UITableViewCell 左滑删除
  13. 复位应答ATR的基本结构和数据元
  14. Android无法导入下载好的项目(和Eclipse中已经存在的项目命名一样导致冲突)解决办法
  15. poj 3411 Paid Roads(dfs)
  16. Myeclipse配置优化
  17. Java代码编写规范(不是标准规范,自行整理,无须纠结)
  18. HTML 简单了解
  19. Opengl4.5 中文手册—E
  20. monkey日志分析

热门文章

  1. poj 1423 打表/斯特林公式
  2. c# 对象 &amp; 类
  3. Mysql 时间格式默认空串 &#39;0000-00-00 00:00:00&#39; select抛出异常的解决方法
  4. Java中获取本地某一个目录下的所有文件和文件夹
  5. python学习交流 - 匿名函数
  6. Redis进阶实践之十 Redis哨兵集群模式
  7. InnoDB索引
  8. c# Nlog 非xml cs方法配置
  9. 使用WinInet实现HTTP站点访问
  10. 归并排序Merge Sort