移動端頁面中有一個search input想監聽搜索鍵盤中的 “搜索按鈕”,看了下keycode是13,但是每次按下回車,回報非法 return 的錯誤
報錯:
Uncaught SyntaxError: Illegal return statement
代碼如下:
<form action="javascript:void 0;"> <input type="text" placeholder="請輸入款號、姓名、手機號、訂單號" class="searchEdit" id="searchEdit" v-model="styleNo" @keyup.13="searchBlur" @input="searchNo($event)" @focus="iptFocus" @blur="iptBlur"/> </form>
searchBlur(){ if(this.styleNo){ this.axios.post('/home/custom/find_order_in_shop',{keywords: this.styleNo}) .then(res => { let result = res.data; if(result.code == '200'){ this.$router.push(`/searchOrder?keywords=${this.styleNo}`); }else{ Toast(result.msg || '無訂單'); } }) .catch(error => { console.log(error); }); } }
報錯原因:
<form action="javascript:return true;"> 里的 action 不對
javascript: 后面要加語句,而 return 是放在函數體中的,不能在其他地方使用
解決方案:
1、action="javascript:void 0"
2、action="javascript:true"
參考文章: vue監聽回車事件報錯的問題