admin管理员组文章数量:1399119
UPDATE: I am constructing the form via Javascript, so the form is not there on page load.
I have an input field with id="input"
, whenever the enter
key is pressed, the form gets submitted. So I tried handling it like this.
$("#input").keydown(function (e) {
if (e.keyCode == 13) {
alert("enter pressed");
return false;
}
});
However this does not work, there is no alert and the form still gets sent. How do I solve this?
UPDATE: I am constructing the form via Javascript, so the form is not there on page load.
I have an input field with id="input"
, whenever the enter
key is pressed, the form gets submitted. So I tried handling it like this.
$("#input").keydown(function (e) {
if (e.keyCode == 13) {
alert("enter pressed");
return false;
}
});
However this does not work, there is no alert and the form still gets sent. How do I solve this?
Share Improve this question edited May 11, 2017 at 15:06 asked May 10, 2017 at 21:26 user7933742user7933742 5-
I think you are referring to the
submit
event – funcoding Commented May 10, 2017 at 21:29 - @funcoding yes exactly – user7933742 Commented May 10, 2017 at 21:35
-
So change your code to use
submit
instead ofkeydown
event – funcoding Commented May 10, 2017 at 21:36 - hitting enter to submit a form, when inside of a text input, is expected behavior for many users. As standard text inputs don't allow for character returns, I'd ask why someone is hitting enter when in one of these inputs to begin with? – scottohara Commented May 11, 2017 at 4:00
- @scottohara right they shouldn't but I want to write a function to prevent this – user7933742 Commented May 11, 2017 at 14:35
5 Answers
Reset to default 2Use preventDefault()
to prevent the form from submitting.
$("#input").keydown(function (e) {
if (e.keyCode == 13) {
alert("enter pressed");
e.preventDefault();
}
});
Example with a form:
$('form').submit(function(e){
e.preventDefault();
});
https://jsfiddle/aya6ockv/
Use onkeypress attribute in your input as follows:
<input type="text" onkeypress="myFunction(event)">
<script>
function myFunction(event) {
var x = event.which || event.keyCode;
if(x == 13) {
alert("enter pressed");
return false;
}
}
</script>
I would do it like this and change 'form' to #input. Unless you want to stop this enter submission site wide, then this as is should work well.
$("form").bind("keypress", function(e) {
if (e.keyCode == 13) {
return false;
}
});
Just created a JSFiddle that shows that it works. Check it out
$('#input').keydown(function (e) {
if (e.keyCode == 13) {
alert('alert pressed');
return false;
}
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="my-form">
<input text="name" id="input">
<button type="submit">Submit</button>
</form>
return false is more efficient than e.preventdefault(). Please look at event.preventDefault() vs. return false
$("#input").keydown(function (e) {
if (e.which == 13) {
alert("enter pressed");
return false;
}
});
本文标签: javascriptHow to disable form submission when enter is pressed on input fieldStack Overflow
版权声明:本文标题:javascript - How to disable form submission when enter is pressed on input field - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744126814a2591990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论