移动端页面中有一个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监听回车事件报错的问题