7

禁止饿了么Element UI的Button 按钮组件的回车事件

 3 years ago
source link: https://www.wikimoe.com/?post=243
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
72551535690738.jpg

禁止饿了么Element UI的Button 按钮组件的回车事件

作者:广树 | 时间:2019-12-8 15:49:32 | 分类 : JavaScript/jQuery/Vue

在Element UI里,如果点击过一次某个按钮,这个按钮就会被选择,然后这个时候按下回车就会连续触发按钮的点击事件,对一些需要服务器交互的按钮来说太过不友善。

根据官方文档,似乎禁不了这个事件,那么只能通过代码去禁止了。

  1. <el-button type="primary" ref="btn">按钮</el-button>

首先给按钮定义一个ref。

  1. mounted() {
  2. this.$refs.btn.$el.onkeydown= (e) =>{
  3. let _key=window.event.keyCode;
  4. if(_key===13){
  5. return false;
然后通过监听回车事件来强行禁止回车事件。
捐赠

评论:

510bc0b0895a1bd801a03098192448b8?s=100&d=mm&r=g&d=robohash
X_NNNNN TA的卡牌
2020-06-03 17:39
借花献佛, @keydown.enter.native.prevent .把这个直接加到el-button的属性上就可以了,不用刻意用ref。
97eb21cf0bacc7c517bd3b9d716c69b1?s=100&d=mm&r=g&d=robohash
野兔 TA的卡牌
2019-12-08 16:00
简单粗暴。
fbb31d99a24cf9a56c48b44dd0797d22?s=100&d=mm&r=g&d=robohash
广树 TA的卡牌
2019-12-08 16:02
@野兔:嗯,用最简单粗暴的方法解决麻烦的问题!

发表评论:

个人昵称

邮件地址 (选填)

个人主页 (选填)

OωO表情

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK