Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript绑定函数未在第一个操作时触发_Javascript_Jquery - Fatal编程技术网

javascript绑定函数未在第一个操作时触发

javascript绑定函数未在第一个操作时触发,javascript,jquery,Javascript,Jquery,嗨,我有以下代码,其中我将一个函数绑定到ladda(ladda库)。除了第一次提交外,一切都正常。对于第一次触发的功能,我需要按两次提交按钮。我认为这与初始化期间没有绑定有关 $(document).ready(function(){ function test(){ Ladda.bind( '#submitPatientAcquisition',{ callback: function( instance ){

嗨,我有以下代码,其中我将一个函数绑定到ladda(ladda库)。除了第一次提交外,一切都正常。对于第一次触发的功能,我需要按两次提交按钮。我认为这与初始化期间没有绑定有关

$(document).ready(function(){
    function test(){
        Ladda.bind( '#submitPatientAcquisition',{
                        callback: function( instance ){
                            console.log("start progress button")
                            $('#patientsLostTable').DataTable().clear().draw()

                            var progress = 0;
                            var completed = false
                            getPatients(yrfrom, yrto).then(function(results){
                                $('#patientsLostTable').DataTable().clear().draw()
                                $('#patientsLostTable').DataTable().rows.add(results["data"]).draw();
                                completed = true
                            }).catch(function(err){
                                console.log(err)
                            })
                            var interval = setInterval( function(){
                                progress = Math.min( progress + Math.random() * 0.05, 1 );
                                instance.setProgress( progress );

                                if( completed ){
                                    instance.stop();
                                    clearInterval(interval);
                                }
                            }, 200 );


                     }
                    }); }

});

绑定需要在documentready的范围内进行,而不是在函数内。函数在被调用之前是不会触发的,因此我需要在document ready中调用函数,或者在函数外部绑定,根据您发布的内容很难判断,但是您可能需要确保首先加载文档:
$(document).ready(function(){//code})它已在文档中准备就绪您确定绑定应该按现有方式使用回调函数吗?您正在为一个对象提供一个名为“回调”的属性,而不是一个实际的函数-可以吗?