我看到这个topic https://stackoverflow.com/questions/2632359/can-jquery-add-commas-while-user-typing-numbers,但是这是Jquery,怎么改成Vue.js呢?是v-on
Vue.js 支持吗?我的错误在哪里?
<div id="vue">
<input v-model="amountModel" v-on:keyup="AddCammas()" value="{{price}}">
</div>
<script>
el: #vue,
methods:{
AddCammas : funtion(){
if(event.which >= 37 && event.which <= 40) return;
$(this).val(function(index, value) {
this.message = this.amountModel
.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
});
}
}
</script>
为此,您根本不需要 jQuery。你watch
您的变量,并在监视函数中计算重新格式化的版本,然后使用将其设置回您的变量nextTick
(所以在手表完成之前它不会发生变化)。
new Vue({
el: '#vue',
data: {
price: 0
},
watch: {
price: function(newValue) {
const result = newValue.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Vue.nextTick(() => this.price = result);
}
}
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/1.0.28/vue.min.js"></script>
<div id="vue">
<input type="text" v-model="price" />{{price}}
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)