jQuery错误 "Uncaught TypeError: Cannot read property 'apply' of undefined"

3
我正在根据选择字段的更改,在jQuery对话框中启用一个“确认”按钮。虽然这部分内容已经生效,但是当我点击“确认”或“关闭”以保存信息并继续时,会出现以下错误。

Chrome: Uncaught TypeError: Cannot read property 'apply' of undefined

Firefox: TypeError: d.click is undefined

... e="button">').click(function(){d.click.apply(c.element[0],arguments)})....
jquery-ui.min.js (line 14, col 5350)

    var disposition;  // I'm using this so that the alert('fixed') doesn't get call on load...  

//  Callback Disposition Dialog
$('#disposition_modal').dialog({
    open: function () {  // removed the default close link
        $(this).parent().children(':first').children('a').remove();
    },
    buttons: [{
        text: 'Cancel',
        Ok: function () {
            $(this).dialog("close");
        }
    }, {
        text: 'OK',
        disabled: true,
        id: 'dm_btn',
        Ok: function () {
            if (disposition !== '' && undefined !== disposition) {
                alert('fixed');
                $(this).dialog("close");
            }
        }
    }]
});

// toggle the OK button 
$('#disposition_id_in2').change(function () {
    disposition = $('#disposition_id_in2').val();
    if ($('#disposition_id_in2').val()) {
        $('#dm_btn').attr('disabled', false);
    } else {
        $('#dm_btn').attr('disabled', true);
    }
});

这是一个关于JSFiddle按钮错误的最简化演示:JSFiddle button error

当使用错误的 jquery-migrate 库版本时,我遇到了相同的错误。从 WordPress 5.6 开始,他们增加了新的 jquery-migrate 版本。 - jave.web
1个回答

3

您需要将按钮上的“Ok”更改为“点击”。

buttons: [{
    text: 'Cancel',
    click: function () {
        $(this).dialog("close");
    }
}, {
    text: 'OK',
    disabled: true,
    id: 'dm_btn',
    click: function () {
        console.log(disposition);
        if (disposition !== '' && undefined !== disposition) {
            alert('fixed');
            $(this).dialog("close");
        }
    }
}]

jsfiddle


虽然你说得没错,但是OP遇到的具体错误与他们的代码没有使用“日历小部件”有关。因此,我认为这是一个更普遍的错误。(当然我可能错了) - Ofir Baruch
@OfirBaruch - 你的浏览器控制台可能显示了一些不正确的内容。(我的似乎是这样的。)如果你打开https://code.jquery.com/ui/1.8.18/jquery-ui.min.js文件并查看第14行,第5350列,那里是对话框按钮的代码。 - John S
谢谢,John S!我怀疑这是一些简单但非显而易见的东西!:^) - marklark
@JohnS,我不知道为什么控制台会标记错误的那行代码。谢谢你告诉我,从今天开始我会对控制台的代码备注持怀疑态度。 - Ofir Baruch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接