鼠标光标在input框,单击回车键后防止页面刷新的问题

问题描述:在页面中有一个input框,在输入完成时单机回车键导致整个页面刷新了

分析原因:这样的页面一般是由一个input框和一个form表单构成的,按下回车键之后就会默认提交表单了。

解决办法:方法一:在页面添加一个input框,样式设置为隐藏

      

<input type="text" style="display: none;">

方法二:给input框添加onkeydown事件阻止事件冒泡

  

<input type="text" onkeydown="checkInfo(event,this)">
<script>
    function checkInfo(event,self){
        if (event.keyCode == 13) {
            event.cancleBubble = true;
            event.returnValue = false;
            return false;
        }
    }
</script>

猜你喜欢

转载自www.cnblogs.com/webgis-ling/p/12971895.html