Reputation: 192
I have two textboxes and one Button control.....In first TextBox when press enter key moves to next textbox(Barcode) and when i press enter in barcode textbox it fires the button click event......till that its ok....
But what happening after fireing the Button click even on enter in Barcode Textbox its going back to focus on first textbox.........But i want this to stay in same Barcode TextBox to scan more barcodes.
The code is below.
<script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">
</script>
<Head>
<script type="text/javascript">
$(function() {
$('input:text:first').focus();
var $inp = $('input:text');
$inp.bind('keydown', function(e) {
//var key = (e.keyCode ? e.keyCode : e.charCode);
var key = e.which;
if (key == 13) {
e.preventDefault();
var nxtIdx = $inp.index(this) + 1;
$(":input:text:eq(" + nxtIdx + ")").focus();
}
});
});
</script>
</Head>
Upvotes: 0
Views: 5946
Reputation: 1
The button click is causing a postback to the server and reloading the page, which re-executes the javascript you posted including this line:
$('input:text:first').focus();
which sets focus on the first text input.
I can't offer much of a solution without the HTML, but hopefully understanding the cause can point you in the right direction.
Upvotes: 0
Reputation: 887413
You're handling keydown
for every textbox.
You need to modify your selector to only handle keydown
for the first textbox.
Upvotes: 2