admin管理员组文章数量:1391751
I try like this :
<template>
...
<input type="number" class="form-control" v-model="quantity" min="1" v-on:keydown="disableDot">
...
</template>
<script>
export default{
...
methods:{
disableDot: function(evt) {
evt = (evt) ? evt : window.event
let charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode === 190 || charCode === 189 || charCode === 187) {
evt.preventDefault()
}
else {
return true
}
}
}
}
</script>
If the code executed and I input dot(.), it still can
In dekstop, it had disable. But in mobile, it does not disable
I want to disable dot. So user can not input dot
How can I do it?
Note
In dekstop, the code works. But in mobile, the code does not works. The dot(.) no disable in mobile
I try like this :
<template>
...
<input type="number" class="form-control" v-model="quantity" min="1" v-on:keydown="disableDot">
...
</template>
<script>
export default{
...
methods:{
disableDot: function(evt) {
evt = (evt) ? evt : window.event
let charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode === 190 || charCode === 189 || charCode === 187) {
evt.preventDefault()
}
else {
return true
}
}
}
}
</script>
If the code executed and I input dot(.), it still can
In dekstop, it had disable. But in mobile, it does not disable
I want to disable dot. So user can not input dot
How can I do it?
Note
In dekstop, the code works. But in mobile, the code does not works. The dot(.) no disable in mobile
Share Improve this question asked Oct 9, 2017 at 6:49 moses tohmoses toh 13.2k81 gold badges265 silver badges459 bronze badges3 Answers
Reset to default 4The trouble is that the keycodes with 'keydown' or 'keyup' dont seem to be consistent across browsers. Maybe the OS has an affect as well. You can test this on various browsers and mobile devices here
I think you will find more consistency using the v-on:keypress
event instead. The following in my quick (inplete on mobile) testing returns '46' consistently. A quick warning, I noticed that typing '.' on firefox mobile on my android keyboard I was receiving two keypress events.
//in template
<input type="number" v-on:keypress="capturePress($event)">
//in methods
capturePress: function(event) {
console.log(event.charCode);
}
I would also encourage you to have a look at the whole event as it also returns. event.code = "Period"
and event.key = "."
though only event.key = "."
was filled on mobile firefox.
console.log(event);
Using a watcher worked the best for me. It would be something like this:
<template>
...
<input type="number" class="form-control" v-model="quantity" min="1">
...
</template>
<script>
export default{
...
methods:{
disableDot: function(evt) {
evt = (evt) ? evt : window.event
let charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode === 190 || charCode === 189 || charCode === 187) {
evt.preventDefault()
}
else {
return true
}
}
}
watch: {
"quantity"(value) {
this.disableDot();
}
},
}
</script>
I found the real issue with android device , You should make disable the autoplete of your form like this:
<form autoplete="off" action="/action_page.php">
本文标签: javascriptWhy keydown not working in mobile vuejs 2Stack Overflow
版权声明:本文标题:javascript - Why keydown not working in mobile? vue.js 2 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744768940a2624217.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论