vue使用element-ui的el-input監聽不了鍵盤事件,原因應該是element-ui自身封裝了一層div在input標簽外面,把原來的事件隱藏了,情況如下:
直接使用標簽:
<input placeholder="賬號" @keyup.enter="doLogin"></input>
element-ui:
<el-input v-model="name" placeholder="賬號" @keyup.enter.native="doLogin"></el-input>
如果你使用了form表單 使用 @keyup.enter.native="doLogin" ,
兩個el-input 鍵盤事件有效, 如:
<el-form>
<el-form-item prop="username">
<el-input name="username" type="text" autoComplete="on" placeholder="郵箱" />
</el-form-item>
<el-form-item prop="password">
<el-input name="password" type="password" @keyup.enter.native="handleLogin" autoComplete="on" placeholder="密碼"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click.native.prevent="handleLogin">登錄</el-button>
</el-form-item>
</el-form>
如果只有一個el-input , 則無效, 需加上@submit.native.prevent才有效,阻值冒泡,默認事件, 如:
<el-form @submit.native.prevent>
<el-form-item>
<el-input placeholder="請輸入內容" @keyup.enter.native="submitImage"></el-input>
</el-form-item>
</el-form>
