admin管理员组文章数量:1414604
I have a HTML form which contains a input text and a button. HTML
<form id="form1">
<input type="text" />
<button onclick='YouClick();'>Click</button>
</form>
JS function
function YouClick() {
alert("You just clicked");
}
When user click the button , the javascript function YouClick
executes. But if i pressed enter key on input text fields than also YouClick
function executes. How can i stop this behaviour ? So that only by clicking the button the YouClick
function executes.
I have a HTML form which contains a input text and a button. HTML
<form id="form1">
<input type="text" />
<button onclick='YouClick();'>Click</button>
</form>
JS function
function YouClick() {
alert("You just clicked");
}
When user click the button , the javascript function YouClick
executes. But if i pressed enter key on input text fields than also YouClick
function executes. How can i stop this behaviour ? So that only by clicking the button the YouClick
function executes.
- 1 Your button is inside the form. So when you are pressing the enter button its being pressed. Put it outside the form and see. Its not a solution. Solutions are given below. I am just telling you the reason – polin Commented Oct 31, 2012 at 10:26
- @NejiHyuga: why would you need a fiddle for that? – Michal B. Commented Oct 31, 2012 at 10:31
2 Answers
Reset to default 4You are submitting the form, then the button which by default is type='submit'
is triggered, you can set the type attribute of the button as button
:
<button type='button' onclick='YouClick();'>Click</button>
This should work :
$(document).ready(function() {
$('input').keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
}
});
});
本文标签: jqueryWithout clicking the button its click event executing javascriptStack Overflow
版权声明:本文标题:jquery - Without clicking the button its click event executing javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745171382a2645995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论