input文本框回车时怎样阻止触发form的表单提交 本帖最后由 shangqiu2012cs 于 2014-02-17 15:53:34 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <form action="1.html" method='get' id='xx'> <input> </form> <script> function init() { function keydownFn(e) { if(e.which===13){ e.preventDefault(); } } var $f = document.getElementById('xx'); $f.addEventListener('keydown', keydownFn); } init(); </script> event.preventDefault ? event.preventDefault() : event.returnValue = false; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title></head><body><form action="1.html" method='get' id='xx'> <input> </form> <script> function init() { document.getElementsByTagName('input')[0].onkeydown=function(e){ var a=e||window.event; if(a.preventDefault){ a.preventDefault(); }else{ a.returnValue=false; } } } init(); </script></body></html>试了下可以 是不是你哪里写错了啊 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title></head><body><form action="1.html" method='get' id='xx'> <input> </form> <script> function init() { document.getElementsByTagName('input')[0].onkeydown=function(e){ var a=e||window.event; if(a.keyCode==13){ if(a.preventDefault){ a.preventDefault(); }else{ a.returnValue=false; } } } } init(); </script></body></html>facuo le <form action="" method="post" onsubmit="return check();"><input type="text" value="" id="sbox"/></form><script src="jquery.min.js"></script><script>$(function(){ $("#sbox").keydown(function(event){ if(event.keyCode==13) { return false;//event.preventDefault(); } }); });function check(){ alert('提交');}</script> event.preventDefault(); 或者return false都可以的 document.getElementById("searchName").onkeydown = function(){ if(event.keyCode == 13) { document.getElementById("search").click(); ----尼玛,问题就是这一句 event.returnValue = false; event.keyCode = 0 ; }}上面不执行document.getElementById("search").click()表单提交就可以被阻止,可是这一句必须要有哇!这可怎么办! 靠,又发现另外一个问题:chrome下keyCode的值不可写! 我靠靠靠,你就不会放到后面的吗?<form action="1.html" method='get' id='xx'> <input> <input type='submit' id='search'> </form> <script> function init() { function keydownFn(e) { if(e.which===13){ e.preventDefault(); var $a = document.getElementById("search"); $a.click(); } } var $f = document.getElementById('xx'); $f.addEventListener('keydown', keydownFn); } init(); </script>是event不可写。 哥,相信我,这样也试过了,还是没用的!event不可写? 原来是这样!!! 转是因为 var $a = document.getElementById("search"); $a.click();我就不明白你为什么要它click. 回车已阻止。<input type='button' id='search'> click这,有没有默认动作?<input type='submit' id='search'> click 这就会执行submit动作。 我发现是document.getElementById("search").click()这里面的alert语句的问题document.getElementById("searchName").onkeydown = function(){ if(event.keyCode == 13) { alert("请输入您要搜索的规则名称!"); ----这里,只要是alert语句,就会表单提交,语句移到后面也 是一样 event.returnValue = false; event.keyCode = 0 ; }}因为window.event是全局唯一的,我猜想,会不会是alert语句弹出对话框之后,我点击时,产生了一个新的event,原来的被修改了.可是在alert语句前后查看event,好像也没有变化!神啊,这到底是怎么回事! 1. 你的 click 是什么页面导致一定需要调用?2. 在submit 的 标签上 加上 onclick="return false;" 当确定要提交时再修改它的onclick 鼠标点击滑动的图片轮播,不是点击哦.是鼠标按住不放往左滑,或者往右划 JS 模态窗口 传入参数调用函数 高手请进!先看代码再说! 求一个验证的代码 php 里写的javascript小问题。在线等待—————————————————————— 如何给‘textarea’赋值并在网页中显示? 怎么样使用IFrame内部按钮刷新iframe所在的页面??? 请问怎么创建和使用二维或多维数组阿? 如何做一个表情符號在编辑框以外,点击插入? 为什么setInterval()方法是最后执行的?而不是按顺序执行的? IE8打印问题!!!! firefox浏览器输入框的输入提示响应回车操作错误
</form> <script>
function init() {
function keydownFn(e) {
if(e.which===13){
e.preventDefault();
}
}
var $f = document.getElementById('xx');
$f.addEventListener('keydown', keydownFn);
}
init();
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<form action="1.html" method='get' id='xx'>
<input>
</form>
<script>
function init() {
document.getElementsByTagName('input')[0].onkeydown=function(e){
var a=e||window.event;
if(a.preventDefault){
a.preventDefault();
}else{
a.returnValue=false;
}
}
}
init();
</script>
</body>
</html>试了下可以 是不是你哪里写错了啊
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<form action="1.html" method='get' id='xx'>
<input>
</form>
<script>
function init() {
document.getElementsByTagName('input')[0].onkeydown=function(e){
var a=e||window.event;
if(a.keyCode==13){
if(a.preventDefault){
a.preventDefault();
}else{
a.returnValue=false;
}
}
}
}
init();
</script>
</body>
</html>
facuo le
<input type="text" value="" id="sbox"/>
</form>
<script src="jquery.min.js"></script>
<script>
$(function(){
$("#sbox").keydown(function(event){
if(event.keyCode==13)
{
return false;//event.preventDefault();
}
});
});
function check()
{
alert('提交');
}
</script>
{
if(event.keyCode == 13)
{
document.getElementById("search").click(); ----尼玛,问题就是这一句
event.returnValue = false;
event.keyCode = 0 ;
}
}上面不执行document.getElementById("search").click()表单提交就可以被阻止,可是这一句必须要有哇!这可怎么办!
<form action="1.html" method='get' id='xx'> <input>
<input type='submit' id='search'> </form> <script>
function init() {
function keydownFn(e) {
if(e.which===13){
e.preventDefault();
var $a = document.getElementById("search");
$a.click();
}
}
var $f = document.getElementById('xx');
$f.addEventListener('keydown', keydownFn);
}
init();
</script>是event不可写。
event不可写? 原来是这样!!!
var $a = document.getElementById("search");
$a.click();
我就不明白你为什么要它click.
<input type='button' id='search'> click这,有没有默认动作?
<input type='submit' id='search'> click 这就会执行submit动作。
document.getElementById("searchName").onkeydown = function()
{
if(event.keyCode == 13)
{
alert("请输入您要搜索的规则名称!"); ----这里,只要是alert语句,就会表单提交,语句移到后面也
是一样
event.returnValue = false;
event.keyCode = 0 ;
}
}
因为window.event是全局唯一的,我猜想,会不会是alert语句弹出对话框之后,我点击时,产生了一个新的event,原来的被修改了.
可是在alert语句前后查看event,好像也没有变化!
神啊,这到底是怎么回事!
2. 在submit 的 标签上 加上 onclick="return false;" 当确定要提交时再修改它的onclick