简体   繁体   English

动态函数调用仅在发出alert()时有效

[英]Dynamic function call only works when an alert() is given

I have a Javascript problem that I cannot find the answer to. 我有一个Javascript问题,找不到答案。 Hope you can help me. 希望您能够帮助我。

I have this element called 'scanValue', that has an onFocus and an onBlur trigger: 我有一个名为“ scanValue”的元素,它具有一个onFocus和一个onBlur触发器:

<input name="scanValue" class="searchFormText" style="width: 220px; font-size: 22px;" onfocus="onFocusElement(this)" onblur="onBlurElement(this)" type="text" maxLength="20" value="510210006823414"/>

If I tab out of the field the onBlurElement() function is called as expected: 如果我跳出该字段,则会按预期方式调用onBlurElement()函数:

function onBlurElement(object) {
        alert('onBlurElement: Start blur on ' + object.name + ' (old val = ' + prevObjectVal + ', new val = ' + object.value + ')');

        if (object.value !== prevObjectVal) {
            check(object);

            var checkFcn = 'check' + object.name;
            var fcnParms = [object.value];
            var fcn = window[checkFcn];

            alert('onBlurElement: check if ' + checkFcn + ' is a function: ' + (typeof fcn));
            if (typeof fcn == 'function') {
                alert('fcnParms length = ' + fcnParms.length + '. ToString= ' + fcnParms.toString());
                alert('fcnParms[0] length = ' + fcnParms[0].length + '. ToString= ' + fcnParms.toString());
                fcn.apply(fcn, fcnParms);
            }
        }
    }

Now this dynamic function call (fcn.apply()) should call the function 'checkscanValue(val)', but nothing happens. 现在,此动态函数调用(fcn.apply())应该调用函数'checkscanValue(val)',但是什么也没有发生。 EXCEPT when I add an alert() to this function OR if I fire up the IE standard developer tools. 除了向该函数添加alert()或启动IE标准开发人员工具时之外。 In other words, if I track or debug the checkscanValue() function everything works, otherwise is does nothing. 换句话说,如果我跟踪或调试checkscanValue()函数,则一切正常,否则不起作用。 I've tried several different things, but nothing seems to work. 我尝试了几种不同的方法,但似乎没有任何效果。 I doubt this could have anything to do with the form being submitted with method post, but maybe someone could help me on that. 我怀疑这可能与方法发布时提交的表单有关,但也许有人可以帮助我。

Code for the checkscanValue() function: checkscanValue()函数的代码:

 function checkscanValue(val) {
        console.info('checkscanValue: start function');
        document.forms[0].airNumber.value = 'test';

        // Check if the scanned value is valid for submitting the form
        if (val[0].length === 15) {
            document.forms[0].submit();
        }
    }

Everything is working fine except that the "val" parameter that you are passing to the checkscanvalue function is the string that the user entered or is there by default. 除了传递给checkscanvalue函数的“ val”参数是用户输入的字符串或默认情况下存在的字符串之外,其他所有命令都工作正常。

So val[0] returns the first character of the string whose length can't be 15 and hence the check fails and nothing happens. 因此val [0]返回字符串的第一个字符,其长度不能为15,因此检查失败,并且没有任何反应。

hope it helps! 希望能帮助到你!

This seems to answer my question: 'console' is undefined error for Internet Explorer 这似乎可以回答我的问题: “控制台”是Internet Explorer的未定义错误

I've been using the function console.info() which is undefined if the console window of IE was never opened. 我一直在使用console.info()函数,如果从未打开IE的控制台窗口,则该函数未定义。 This caused the function to stop. 这导致功能停止。 If I replaced it with an alert() it obviously worked and if I opened the console of IE, the console.info function was defined. 如果我将其替换为alert(),则显然可以正常工作;如果我打开IE的控制台,则会定义console.info函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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