jQuery UI - Error: cannot call methods on dialog p

2019-03-15 18:28发布

This question already has an answer here:

[Solved] I write this script. Unfortunately, jQuery console throw:

Error: cannot call methods on dialog prior to initialization; attempted to call method 'open'

I use jQuery 1.10.2 and jQuery UI 1.10.4.

 $(function() {
$("#player").on('click', 'img', function() {
    var zadanie = $( "input[name^='act']:checked:enabled" ).val();
    switch(zadanie){
        case '1':
        alert('fajowo jesteś uczciwy');
        break;
        case '2':
         $( "#dialog" ).dialog( "open" );
        break;
    }
    });
});

Dialog in HTML:

<div id="dialog" title="Wybór karty, która zostanie położona oficjalnie">
<p>Fajowy tekst.</p>
</div>

3条回答
祖国的老花朵
2楼-- · 2019-03-15 19:23

You are calling the open method before the Dialog Widget has been initialized for more information go here: https://api.jqueryui.com/dialog/

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-15 19:25

Set it up first:

$("#dialog").dialog();

Try it yourself: http://jsfiddle.net/x5w8r/

查看更多
狗以群分
4楼-- · 2019-03-15 19:30

You have to initialize the dialog before being able to open it:

$(function() {
    $("#dialog").dialog({
        autoOpen: false
    });

    $("#player").on('click', 'img', function() {
    ...

Here's a complete example: https://jqueryui.com/dialog/#animated

查看更多
登录 后发表回答