简体   繁体   中英

Form with multiple submit buttons, how to set the default enter key action - barcode scanner input

I've been stuck on this issue. I have a simple form:

<form>
<input type="text" name="barcode1">
<input type="submit" name="barcode1" value="Submit Barcode1">
<input type="text" name="barcode2">
<input type="submit" name="barcode2" value="Submit Barcode2">
</form>

I'm using a barcode scanner to enter the data into the text field. The scanner records a series of numbers, followed by a 'Enter' command.

When I scan into the barcode1 field, it works perfectly, the form is submitted by the scanner's 'Enter' command.

When I scan into barcode2 field, it submits the form using the barcode1 submit.

Is it possible, to change the default submit button on a form with multiple submit buttons? Perhaps I could set the default submit button when I focus on the text field for that submit button.

Something like this?

$(document).ready(function() {
    var toClick;
    $(":text").focus(function() {
        toClick = $(this);
    });
    $('form').keypress(function(event) {
        if (event.keyCode == 13) {
            event.preventDefault();
            $(toClick).next('input[type="submit"]').click();
        }
    });

});

Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM