0

如果我调用$('#txt1').val()or $('#txt1').text()or $('#txt1').html(),这是否会将 thefocus设置为该元素?因为,我有这个focusout处理程序,它被称为n时间(n网格中的行数),即使我只在 txtbox 中单击一次,输入一个值,按下选项卡,从而确保只focusout发生一次(至少从什么我是手动做的)。但是n有时,有一个功能正在阅读$('#txt1').val(),所以我猜这就是必须将焦点设置在文本框上并导致失去焦点的原因。

所以,简而言之,即使没有明确声明/聚焦,调用是否val(), text() or html()会将焦点设置为元素?

ps:我什至可以发布代码,但相信我,这真的太长了,不能在这里发布,但是,如果有人需要,请评论!

** 编辑 ** 发布最少可能的代码....

var txtDiscountFocusOutHandler = function (event) {
            if ($(this).val() == '') {
                $(this).val('0');
            }
                recomputeAllGrid(1, ($('#grdEntry > tbody > tr').size()), false, true);
            }

        };

        // Step 20.D don't add the handler in the body,
        // add on keydown, that means user has explicitly changed the discount
        var txtDiscountKeyDownHandler = function (event) {
            // attach the handler here
            $('body').on("focusout.AttachedEvent", '#txtDiscount', txtDiscountFocusOutHandler);
        };



        // step 21. this will recalculate all the data of lower grid
        // argStart = the starting number to being counting from
        // argEnd = the row to stop count at
        // argCallEachRowAsUpdate = call row as updadte or not
        // argAddDisAndTax = should dis and tax be added or subtracted?
        function recomputeAllGrid(argStartRow, argEndRow, argCallEachRowAsUpdate, argAddDisAndTax) {
            var _iCounter;
            // remove the event handler
            $('body').off("focusout.AttachedEvent", '#txtDiscount', txtDiscountFocusOutHandler);
            // set the lblDiscount = 0;
            $('#lblDisAmt').text('0');
            // set the tax = 0
            $('#txtSrTax').val('0');
            // set the total to 0
            $('#txtTotal').val('0');
            // don't change the value of advance
            // change the value of balance
            $('#txtBalance').val('0');
            // clear the alltax, and alldiscount
            $('#hdnAllTax').val('');
            $('#hdnAllDiscount').val('');
            //$('#hdnAllDiscountFocusOut').val('0');
            // for the given number of rows
            var _reCompProcessArray, _reCompQty;
            var _reCompPrc, _reCompRate, _reCompPrc1, _reCompRate1, _reCompPrc2, _reCompRate2;
            var _reCompAllSplittedPrcRate;

            for (_iCounter = argStartRow; _iCounter < argEndRow; _iCounter++) {
                // find qty
                _reCompQty = $('body').find('#grdEntry > tbody').find('#Qty_' + _iCounter + '').text() + '';
                // find the process array for each
                var _reCompProcessArray = $('body').find('#grdEntry > tbody').find('#Prc_' + _iCounter + '').text() + '';
                _reCompAllSplittedPrcRate = splitPrcRateFromArray(_reCompProcessArray);
                // set the variables
                _reCompPrc = _reCompAllSplittedPrcRate.prc;
                _reCompPrc1 = _reCompAllSplittedPrcRate.prc1;
                _reCompPrc2 = _reCompAllSplittedPrcRate.prc2;
                _reCompRate = _reCompAllSplittedPrcRate.rate;
                _reCompRate1 = _reCompAllSplittedPrcRate.rate1;
                _reCompRate2 = _reCompAllSplittedPrcRate.rate2;
                // find the disAndTax
                ComputeRowDisTaxAmt(_reCompPrc, _reCompRate, _reCompPrc1, _reCompRate1, _reCompPrc2, _reCompRate2, _reCompQty, false, -1, true);
            }
            // reset all values
            setTheDefaults();
            $('#grdEntry_ctl01_lblHAmount').text($('#txtCurrentDue').val());

        };

        // Step 6.B this function splits the array passed of rate and prc into individuals
        // the format of ary is : (prc@rate),(prc1@rate1), and so on
        function splitPrcRateFromArray(argPrcRateArray) {
            var firstPrc, firstRate, SecondPrc, SecondRate, ThirdPrc, ThirdRate;

            if (argPrcRateArray.indexOf(',') > 0) {
                // there is more than 1 process
                var PrcNameAndRateArray = argPrcRateArray.split(',');

                // split 3 times
                // first rate and prc
                firstPrc = PrcNameAndRateArray[0].split("@")[0].substring(1) + '';
                firstRate = PrcNameAndRateArray[0].split("@")[1] + '';
                firstRate = firstRate.substring(0, firstRate.length - 1);

                // second rate and prc
                SecondPrc = PrcNameAndRateArray[1].split("@")[0].substring(1);
                SecondRate = PrcNameAndRateArray[1].split("@")[1];
                SecondRate = SecondRate.substring(0, SecondRate.length - 1);

                // third rate and prc
                ThirdPrc = PrcNameAndRateArray[2].split("@")[0].substring(1);
                ThirdRate = PrcNameAndRateArray[2].split("@")[1];
                ThirdRate = ThirdRate.substring(0, ThirdRate.length - 1);

                // check for null
                if (firstPrc == '') { firstPrc = '' };
                if (SecondPrc == '') { SecondPrc = '' };
                if (ThirdPrc == '') { ThirdPrc = '' };
                if (firstRate == '') { firstRate = '0' };
                if (SecondRate == '') { SecondRate = '0' };
                if (ThirdRate == '') { ThirdRate = '0' };
            }
            else {
                // there is just one item
                firstPrc = argPrcRateArray.split("@")[0].substring(1) + '';
                firstRate = argPrcRateArray.split("@")[1] + '';
                firstRate = firstRate.substring(0, firstRate.length - 1);
                SecondPrc = '';
                SecondRate = '0';
                ThirdPrc = '';
                ThirdRate = '0';
            }
            // json object returned
            var _returnValue =
             { 'prc': firstPrc,
                 'prc1': SecondPrc,
                 'prc2': ThirdPrc,
                 'rate': firstRate,
                 'rate1': SecondRate,
                 'rate2': ThirdRate
             };
            return _returnValue;
        }



 function ComputeRowDisTaxAmt(argPrc, argRate, argPrc1, argRate1, argPrc2, argRate2, argQty, argIsUpdating, argRowNumber, argIsRecomputing) {
            var _curPrcDis = 0;
            var _curPrcTax = 0;
            var _curPrcAmount = 0;
            var _curPrcDis1 = 0;
            var _curPrcTax1 = 0;
            var _curPrcAmount1 = 0;
            var _curPrcDis2 = 0;
            var _curPrcTax2 = 0;
            var _curPrcAmount2 = 0;
            var _curDisStr = '';
            var _curTaxStr = '';
            var _iCounter;
            var _totalDiscOfRow = 0;
            var _totalTaxOfRow = 0;

            // First time
            _curPrcDis = findDiscountForProcess(argPrc, argRate, true);
            _curPrcAmount = findAmountForProcess(argRate, argQty, true);
            _curPrcTax = findTaxForProcess(argPrc, _curPrcAmount, _curPrcDis);
            _curDisStr = _curPrcDis + '';
            _curTaxStr = _curPrcTax + '';
            // two more times
            // other checks irrelevant to this question
            }

         /********************************************************/
        // now the function that _I think_ is causing this trouble
        // the reason I think so, is because it is calling _val()_
        // and I think that might be setting focus to discount box
        // and so its being called _n_ times

        // Step 3.D Find row discount
        // It caculates the discount of current row/process
        // argPrc : The process
        // argPrcRate : Rate of the Process
        // bApplyQty : Indicates weather to take qty into accout, that would be the case in for first process
        function findDiscountForProcess(argPrc, argPrcRate, bApplyQty) {
            // if prc is null, just return 0
            if (argPrc == '') { return 0; };
            // find the rate in discount percentage
            var _discountPerc = $('#txtDiscount').val();
            //alert($(document.activeElement).attr('id'));
            var _qty = parseFloat($('#txtQty').val());
            var _amtToCalcDisOn;
            _amtToCalcDisOn = findAmountForProcess(argPrcRate, _qty, bApplyQty);
            return parseFloat(_amtToCalcDisOn) * _discountPerc / 100;
        };
4

1 回答 1

0

好的!我找到了罪魁祸首。这是执行n时间(网格中的行数)的原因是因为我在事件中绑定了它(focusout事件)keydown。我仍然无法弄清楚它发生的原因。但是,一旦我将其从keydownof 中删除txtDiscount,并将其放入(在它之前所在位置$('body').on("focusout.AttachedEvent", '#txtDiscount', txtDiscountFocusOutHandler);body外部keydown,一切都完美无缺,它只被调用一次!但不知道为什么!

于 2012-09-18T06:11:52.117 回答