项目中使用了elementU发现使用form表单组件,用户输入后,按‘enter’键后页面刷新;并且地址栏中的所有参数都丢失;
首先全局禁用了enter键
// 禁止enter键提交表单
// document.onkeydown = function(event)
// {
// e = event ? event : (window.event ? window.event : null);
// if(e.keyCode==13){
// //执行的方法
// alert('回车检测到了');
// }
// }
后来产品又要求用户要使用enter键;(哎!头大啊)
(1)禁止输入内容后刷新页面
<el-form @submit.native.prevent></el-form>
(2)弹出框触发keydown事件(el-dialog元素是绑定不了事件的;所以新添加div元素;给div元素绑定事件;div默认是得不到焦点的,可以为其设置tabindex属性使其可以获得焦点。也就可以绑定键盘事件。)
<div @keydown="enterFun($event,'hirePurchaseFun')" tabindex="0"></div>
//enter键触发事件
enterFun(e,fun){
if(e.keyCode==13){
if('fun'=='zhifubaoFun'){
this.getPayPos('EBC19',1)()
}
else if('fun'=='weixinFun'){
this.getPayPos('EBC18',1)()
}
else if('fun'=='meifubaoFun'){
this.getPayPos('EBC20',1)
}
else{
this[fun]()
}
}
},
最后的代码如下:
一个产品需求;学到这么多知识!