javascript禁用回车键 防止回车表单提交

<script type="text/javascript">
//注册键盘事件
document.onkeydown = function(e) {
    //捕捉回车事件
    var ev = (typeof event!= 'undefined') ? window.event : e;
    if(ev.keyCode == 13 && document.activeElement.id == "msg") {
        alert("获取到内容:" + document.activeElement.value);
         return false;//禁用回车事件
    }
}
</script>
 
 
<form action="form.php">
<input type="text" id="msg" name="msg" value="">
<input type="submit" value="submit">
</form>

 

posted on 2016-11-18 11:24  小乔流水人家  阅读(238)  评论(0)    收藏  举报

导航