简体   繁体   English

如何在此函数中包含 window.load 或 $( document ).ready() ?

[英]How include window.load or $( document ).ready() in this function?

Please, I dont have enough javascript skills to modify this code.拜托,我没有足够的 javascript 技能来修改此代码。

The function is triggered when blur change keyup events are fired.该函数在blur change keyup事件被触发时触发。 But ALSO I need the function triggered on window.load or $( document ).ready() .但我还需要在window.load$( document ).ready()上触发的函数。

Someone please can change the code below to work in this scenario?有人可以更改下面的代码以在这种情况下工作吗?

$cep.on('blur change keyup', function() {
    var val = $cep.val();
    // Remove caracteres que o usuario normalmente digita no cep como - e .
    val = val.replace(/\-|\./g, "");
    if (val && currentCep !== val && val.length === 8) {
        currentCep = val;
        self.sendRequest();
    }
});

I think you need to read about function , which its can resolve issue, and its one of basic programming rule, so that your issue will be resolved by this:我认为您需要阅读function ,它可以解决问题,以及它的基本编程规则之一,以便您的问题将通过以下方式解决:

$cep.on('blur change keyup', function() {
    myFunctionName();
}); 

$(myFunctionName); 
// its the same $( document ).ready(myFunctionName);

    function myFunctionName(){
var val = $cep.val();
        // Remove caracteres que o usuario normalmente digita no cep como - e .
        val = val.replace(/\-|\./g, "");
        if (val && currentCep !== val && val.length === 8) {
            currentCep = val;
            self.sendRequest();
        }
}

Last thing, you can try and post your issue when trying code, nested of ask to write code, since its not truth in this community.最后一件事,您可以在尝试代码时尝试发布您的问题,嵌套请求编写代码,因为它在这个社区中不是事实。 good luck.祝你好运。

@thanks for CertainPerformance & Shikkediel @感谢CertainPerformance 和 Shikkediel

Base on Anees answer, I made it work this way, without changing the original code:根据 Anees 的回答,我以这种方式工作,而不更改原始代码:

<script type="text/javascript">
    $(document).ready(function(){
        document.getElementById("cep").focus();    
        document.getElementById("cep").blur();    
    });
</script>

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

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