I'm attempting to run some javascript code when the 'Enter' key is pressed while the focus is on a particular textbox. If this occurs, I DO NOT want the page to postback. (However, if someone clicks the Submit button, then I want the page to postback as it normally would.)
I have a page with the following content:
<script type="text/javascript">
$(function () {
$('#txtInput').on('keyup', function (e) {
if (e.keyCode == 13) {
alert('enter was clicked');
return false;
}
});
});
</script>
<asp:TextBox ID="txtInput" runat="server" ClientIDMode="Static"></asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmitClick" Text="Submit" />
I always thought that return false;
was the way to accomplish this, but in this scenario, it isn't working.
I have also tried the following in my javascript function, to no avail.
$(function () {
$('#txtInput').on('keyup', function (e) {
if (e.keyCode == 13) {
e.cancel = true;
e.returnValue = false;
alert('enter was clicked');
}
});
});
My questions are:
1. Why doesn't this work?
2. How can I achieve this behavior?