For me, it was a problem with the kendo
that I was using in my aspx
page. I was specifying a class on an input
that later was being made as a combobox
by kendo
but the html that kendo was generating was different and it contained that class
on parent element as well. For example I had this control in my aspx
:
<input id="txtNameF" class="submitOnEnter" />
And the generated html in the browser looked like this:
<span class="k-widget k-combobox k-header submitOnEnter" style="width: 191px;">
<span unselectable="on" class="k-dropdown-wrap k-state-default">
<input class="k-input submitOnEnter" type="text" autocomplete="off" tabindex="1" style="width: 100%;">
<span unselectable="on" class="k-select">
<span unselectable="on" class="k-icon k-i-arrow-s">select</span>
</span>
</span>
<input id="txtNameF" class="submitOnEnter" data-role="combobox">
</span>
So now, if you see I have the submitOnEnter
class on the parent span
element as well as the original element that it was added to. That is the reason when the input
control is in focus then it will trigger the keyup
/keydown
/keypress
event for that input as well as the parent element. To eleminate the issue, you can put a boolean
flag that will check if it is the second time it is being triggered. For example:
var alreadyTapped = false;
$('.submitOnEnter').on('keyup', function(e) {
if (e.key === "Enter") {
if (!alreadyTapped) {
alreadyTapped = true;
//dostuff
} else {
alreadyTapped = false;
}
e.preventDefault();
}
});
Or perhaps, you can remove the same class from your parent element on page load.