简体   繁体   中英

ASP.NET postBack post Enter Textbox on javascript

I have a script in js that searches inside a textBox of a gridview that works very well, the problem I find, however, every time I run this function when I press Enter because it always triggers the postback by executing code that you don't always need to run. this is the gridView field -> onfocus = "run_routine (this)":

<input style="font-weight: bold; font-size:14px; text-align: center" id="arrivo" class='nome_<%# Eval("BOD_ABARCODE")%> colored form-control' data-barcode='<%# Eval("BOD_ABARCODE")%>' maxlength="18" name='<%# Eval("BOD_KCODPRU").ToString +"-" + Eval("BOD_KCODCAB").ToString %>' onkeyup="enter(this)" oncopy="return false" oncut="return false" onfocus="esegui_routine(this)" onkeypress="return CheckNumericValue(event)" size="5" type="text" value='<%# Eval("BOD_SQUANTIT")%>' />

below an extract of the function in js where I should insert preventDefault but I have tried everywhere, and it doesn't work for me:

function esegui_routine(oggetto)
            {
                var barcode = $(oggetto).attr('data-barcode');

                var valoreIniziale = $(oggetto).val();

                $(document).removeData();
                $(document).data('valoreIniziale', valoreIniziale);

                $(oggetto).select();

                $(oggetto).off('keypress').on('keypress', function (event) {
                    if (event.which == '13') {

                        var inserito = $(this).val();

                        if (inserito.length != 0) {
                            if (inserito.length <= 4) {
                                var elementi = $.fn.doppio(barcode);

                                if (elementi == 0) {
                                    //BARCODE NON TROVATO : play sound e rimetto il valore precedente cancellando il barcode inserito                                    
                                    $.fn.playSound();
                                    //devo rimettere il valore precedente
                                    $(oggetto).val($(document).data('valoreIniziale')).select();

                                }
                                else if (elementi == 1) {
                                    console.log('barcode: ' + barcode);
                                    $.fn.settaValore(barcode, inserito);
                                }
                                else if (elementi > 1) {
                                    var kcodpru = oggetto.name;
                                    console.log('kcodpru: ' + kcodpru);
                                    $.fn.settaValoreDaKcodpru(kcodpru, inserito);                                    
                                }
                                //console.log('eccomi minore di 4')
                            }
                        }
                    }
                });

Thank you and good job to everybody

I guess you are pressing a submit button, that causes form to post back. Use a instead of

<button id = "btnSubmit">Submit</button>

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