JQuery сделать двойной код запуска на Focusout в функции - PullRequest
1 голос
/ 06 ноября 2019

У меня проблема с jquery, мне нужно получить каждое событие при фокусировке ввода

Это мой code.js

var checkFocusout = "";
    $("input").on("focusout", function(){

        checkFocusout = $(this).attr('name');
        console.log(checkFocusout);
    });

Это моя форма enter image description here

Сначала я ввожу значение в первый вход. enter image description here

Это моя консоль в браузере

enter image description here

Так и должно быть.

Но я ввожу значение для второго ввода

enter image description here

И эту консоль в браузере.

avas

В консоли нужно делать один раз, но это делает двойное время

Мой HTML

<div class="col-md-6">
        <div class="form-group row"><label class="col-sm-4 col-form-label">Number of Adult</label>
            <div class="col-sm-8"><input type="number" name="noa_2" class="form-control text-right" value="0"
                                         onchange="calculatePrice(2)" price="111"></div>
        </div>
        <div class="form-group row"><label class="col-sm-4 col-form-label">Number of Child</label>
            <div class="col-sm-8"><input type="number" name="noc_2" class="form-control text-right" value="0"
                                         onchange="calculatePrice(2)" price="222"></div>
        </div>
        <div class="form-group row"><label class="col-sm-4 col-form-label">Number of Infant</label>
            <div class="col-sm-8"><input type="number" name="noi_2" class="form-control text-right" value="0"
                                         onchange="calculatePrice(2)" price="0"></div>
        </div>
        <div class="form-group row"><label class="col-sm-4 col-form-label">Discounts</label>
            <div class="col-sm-8"><input type="number" name="d_2" class="form-control text-right" value="0"
                                         onchange="calculatePrice(2)"></div>
        </div>
        <div class="form-group row"><label class="col-sm-4 col-form-label">Total</label>
            <div class="col-sm-8"><input type="number" name="t_2" readonly="" class="form-control text-right"></div>
        </div>
        <div class="form-group row"><label class="col-sm-4 col-form-label">Vat</label>
            <div class="input-group col-sm-8">
                <div class="input-group-prepend">
                    <div class="input-group-text">%</div>
                </div>
                <input type="number" name="v_2" class="form-control text-right" value="7" onchange="calculatePrice(2)">
            </div>
        </div>
        <div class="form-group row"><label class="col-sm-4 col-form-label">Net Total</label>
            <div class="col-sm-8"><input type="number" name="nt_2" readonly="" class="form-control text-right"></div>
        </div>
    </div>

Мой полнофункциональный JavaScript

function calculatePrice(product_id) {
    var noa = parseInt($('input[name="noa_'+product_id+'"]').val());
    var noc = parseInt($('input[name="noc_'+product_id+'"]').val());
    var noi = parseInt($('input[name="noi_'+product_id+'"]').val());
    var total_pax = (noa+noc+noi)+parseInt(available_of_product[product_id]);
    var vat = parseInt($('input[name="v_'+product_id+'"]').val());
    var t = parseInt($('input[name="t_'+product_id+'"]').val());
    var nt = parseInt($('input[name="nt_'+product_id+'"]').val());
    var public_adult = parseInt($('input[name="noa_'+product_id+'"]').attr('price'));
    var public_child = parseInt($('input[name="noc_'+product_id+'"]').attr('price'));
    var public_infant = parseInt($('input[name="noi_'+product_id+'"]').attr('price'));
    var number_of_pax = $("#available_span_"+product_id).attr('number_of_pax');
    var checkFocusout = "";


    $("input").on("keyup", function(){

        checkFocusout = $(this).attr('name');
        console.log(checkFocusout);
    });
    if (total_pax > number_of_pax){
        $("#alertMessageBody").html('<p>The number of passengers exceeds the maximum number of products.</p>');
        $('#alertMessage').modal('show')
    }else{

    }
}

1 Ответ

1 голос
/ 06 ноября 2019

Вы можете сделать это

$("input").unbind().on("focusout", function(event){
        console.log(i)
        checkFocusout = $(this).attr('name');
        console.log(checkFocusout);
        i=i+1
    });

Я надеюсь, что это будет работать для вас

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...