未捕获的错误:jQuery.modal上不存在方法show

Jes*_*vya 4 html javascript jquery twitter-bootstrap bootstrap-modal

所以我试图强制保持引导模式显示甚至在切换按钮上单击使用$('#myModal').modal('show'); show方法在外部.click()函数时工作,但是当它在.click()函数内时没有说这样的方法

因此

$('document').ready(function() {

    $('#my-toggle-button').click(function() {   
        $('#myModal').modal('show'); // Throws up the below error
    });

});
Run Code Online (Sandbox Code Playgroud)

错误:

Uncaught Error: Method show does not exist on jQuery.modal
at Function.error (jquery-2.2.4.min.js:2)
at n.fn.init.a.fn.modal (materialize.min.js:8)
at HTMLAnchorElement.<anonymous> (myScript.js:4)
at HTMLAnchorElement.dispatch (jquery-2.2.4.min.js:3)
at HTMLAnchorElement.r.handle (jquery-2.2.4.min.js:3)
Run Code Online (Sandbox Code Playgroud)

但是它工作正常

$('document').ready(function() {

    $('#myModal').modal('show'); // Works fine

    $('#my-toggle-button').click(function() {   

    });

});
Run Code Online (Sandbox Code Playgroud)

更新: 原因是由于脚本导入具有延迟和aysnc而导致它.我不知道什么是正确的地方使用这些因为他们不认识莫代尔.

Fer*_*eón 12

当您还在使用materializecss时尝试执行引导模式时会出现此问题,最好的建议是使用materializecss模式.

http://materializecss.com/modals.html

//initialize all modals
$('.modal').modal({
    dismissible: true
});

//call the specific div (modal)
$('#modal2').modal('open');
Run Code Online (Sandbox Code Playgroud)

请享用!