vue3+ts+element-plus 表單el-form取消回車默認提交
問題描述:在表單el-form中的el-input中按回車后,頁面會刷新,url也會改變,
回車前:

回車后:

相關代碼:

解決方法1:在 el-form 上阻止默認的 submit 事件,增加 @submit.prevent,在 el-form 上監(jiān)聽 submit 事件,并調用 event.preventDefault() 來阻止默認的提交行為。

解決方法2:在 el-form 上阻止默認的 submit 事件,增加 @submit.native.prevent,在 el-form 上監(jiān)聽 submit.native 事件,并調用 event.preventDefault() 來阻止默認的提交行為。

解決方法3:在 el-form 上阻止 keydown 回車事件,增加 @keydown.enter.prevent,在 el-form 上監(jiān)聽 keydown.enter 事件,并調用 event.preventDefault()來阻止默認的回車行為。

解決方法4:在 指定的 el-input 組件上阻止 keydown 回車事件,增加 @keydown.enter.prevent,在 el-input 上監(jiān)聽 keydown.enter 事件,并調用 event.preventDefault()來阻止默認的回車行為。

擴展:
經(jīng)過上述調整后,在el-input中按回車后不會默認提交表單了,但還需要實現(xiàn)在el-input中按回車后進行查找(相當于點擊后面的查找按鈕)

修改后的代碼:
<!-- 使用 @keydown.enter.prevent 或 @submit.prevent 或 @submit.native.prevent 取消回車默認提交 -->
<el-form inline style="height: 32px;" @keydown.enter.prevent>
<el-form-item>
<!-- 使用 @keydown.enter="onSearchClick" 按回車進行查找 -->
<!-- 使用 @keydown.a.enter.b="onSearchClick" 按a鍵、回車鍵、b鍵都可以進行查找,注意:= 左邊的內(nèi)容不能使用大寫字母 -->
<el-input v-model="name" placeholder="請輸入查找內(nèi)容" clearable @keydown.enter="onSearchClick">
<template #append>
<el-button :icon="Search" @click="onSearchClick" />
</template>
</el-input>
</el-form-item>
</el-form>@keydown.enter="onSearchClick"
@keydown.a.enter.b.c.d……="onSearchClick" 按a鍵、回車鍵、b鍵、c鍵、d鍵都可以進行查找,注意:= 左邊的內(nèi)容不能使用大寫字母
到此這篇關于vue3+ts+element-plus 表單el-form取消回車默認提交的文章就介紹到這了,更多相關vue3+ts+element-plus 表單el-form取消回車內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue鼠標hover(懸停)改變background-color移入變色問題
這篇文章主要介紹了vue鼠標hover(懸停)改變background-color移入變色問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
解決vue更新路由router-view復用組件內(nèi)容不刷新的問題
今天小編就為大家分享一篇解決vue更新路由router-view復用組件內(nèi)容不刷新的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vue2.0實現(xiàn)列表數(shù)據(jù)增加和刪除
這篇文章主要為大家詳細介紹了vue2.0實現(xiàn)列表數(shù)據(jù)增加和刪除,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06

