We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
https://blog.csdn.net/hope_It/article/details/103180158
日常工作中,用Vue的时候,经常遇到input框只能输入数字的要求
input
可以用,onkeypress事件结合String.fromCharCode去实现
onkeypress
String.fromCharCode
<el-input v-model="name" onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)));"/>
要兼容firefox,则要做兼容:
<el-input v-model="name" onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode || event.which))) || event.which === 8"/>
但是input多了,并且其它地方都需要使用,所以,就准备写个directive方便使用
directive
onkeypress.js
export default { bind: function(el, binding) { el.onkeypress = function(e) { const ev = e || event return (/[\d]/.test(String.fromCharCode(ev.keyCode || ev.which))) || ev.which === 8 } } }
index.js
import VueOnkeypress from './onkeypress.js' const install = function(Vue) { Vue.directive('VueOnkeypress', VueOnkeypress) } if (window.Vue) { window.VueOnkeypress = VueOnkeypress Vue.use(install) } VueOnkeypress.install = install export default VueOnkeypress
局部使用
<el-input v-vue-onkeypress v-model="name"/> import VueOnkeypress from '@/directive/vue_onkeypress/index.js' export default { directives: { VueOnkeypress } }
全局使用
import VueOnkeypress from 'src/directive/onkeypress/index' Vue.directive('VueOnkeypress', VueOnkeypress);
The text was updated successfully, but these errors were encountered:
No branches or pull requests
https://blog.csdn.net/hope_It/article/details/103180158
需求
日常工作中,用Vue的时候,经常遇到
input
框只能输入数字的要求可以用,
onkeypress
事件结合String.fromCharCode
去实现要兼容firefox,则要做兼容:
但是
input
多了,并且其它地方都需要使用,所以,就准备写个directive
方便使用directive
指令的代码实现onkeypress.js
index.js
局部使用
全局使用
The text was updated successfully, but these errors were encountered: