2014-01-11 68 views
2

我试过很多方法,我不知道我错过现在停车回车键单击按钮

<input id="stopBuy" class="grn" type="button" onclick="selectGT()" value="Buy Now"> 

问题在这个时候我们我们按钮点击一次,如果鼠标移动到按钮,然后我们按进​​入它再次点击。所以我想知道是否有办法阻止它?

我试图把它放在整个容器中,但不起作用。我已经包括jQuery的v1.8.3

<script> 
$('#container').keypress(function(e) { // even try putting #stopBuy 
    if(e.which == 13) { 
     e.preventDefault(); 
    } 
}); 
</script> 

回答

4

尽量把你的代码中的DOM准备$(document).ready(function() { });$(function() { }),使其工作:

$(document).ready(function() { 
    $('#container').keypress(function(e) { // even try putting #stopBuy 
     if(e.which == 13) { 
      e.preventDefault(); 
     } 
    }); 
}); 
+0

'变种键= e.which || e.keyCode;如果(key === 13){' –

+0

'$(document).ready(function(){});'这个结果非常感谢。不确定它是否可以在所有浏览器中运行。但我会检查谢谢@Felix – Allison

+0

@Allison是的。它将在每个现有的浏览器中工作。 –

0

也许如果你停止事件传播?

$('#container').keypress(function(e) { // even try putting #stopBuy 
    if(e.which == 13) { 
     e.preventDefault(); 
     e.stopPropagation(); 
    } 
}); 
0

模糊被点击后的按钮:

$('#stopBuy').click(function() { 
    $(this).blur(); 
});