javascript - jquery 在函数中的 Focusout 上执行双重触发代码

标签 javascript jquery

我有一个 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{

    }
}

最佳答案

你可以这样做

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

希望对你有用

关于javascript - jquery 在函数中的 Focusout 上执行双重触发代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58725943/

相关文章:

javascript - 将事件处理程序分配给元素集合

javascript - 模拟谷歌地图双击

javascript - 更改 Date() 对象 jQuery 的时间

javascript - javascript中测试当前线程是否为主线程

javascript - 检查填充 src 后视频何时开始播放

javascript - 类型错误 : Cannot read property '0' of undefined for an array already defined in google scripts

javascript - jquery 未捕获语法错误 : Unexpected token -

javascript - 我如何确定在第一次加载时是否在 jQuery 中单击了某些内容?

javascript - 使用mapReduce在mongodb中检索总和以及计数

javascript - Jquery 无法在 Dreamweaver 中运行。不确定链接是否正确