JavaScript错误说"确认不是函数"

Key*_*ger 6 validation jquery jquery-ui jquery-plugins

我在提交表单时使用jQuery和验证插件:

$('.frmProject:visible').validate( {

    errorContainer: ".site_details:visible .messageBox1",
    errorLabelContainer: ".site_details:visible .messageBox1 span.messagehere",
    invalidHandler: function(form, validator) {
    },

    rules: {
        site_id: {
            required: true,
        }
    },
    messages: {
        site_id: "Project has no assigned site information. Click the marker on the map at left to specify the site where this project took place."
    },
    submitHandler: function(data) {
        SaveProject();
    }

});
Run Code Online (Sandbox Code Playgroud)

在submitHandler中,

function SaveProject(){
    //... load variables with input contents
    $.ajax({
        url: 'ajax/save_project.php',
        dataType: 'json',
        data: 'id='+id+'&title='+title+'&project='+project+'&sector='+sector+'&volunteer='+volunteer+'&lat='+lat+'&lng='+lng+'&name='+name+'&mun='+mun+'&prov='+prov,
        success: function(data) {
            //... load 'messages' object with stuff
            $.each(messages, function(key, value) {
                if (confirm(key)){
                    console.log(item);
                }
            });
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

当我提交经过验证的表单并在每个循环中进入确认时,我收到错误消息:"确认不是函数".

如何向用户显示消息以进行确认?

编辑:

当我在控制台中键入"确认"时,我得到了这个:

控制台输出的屏幕截图

检查DOM显示:

window> confirm()此对象没有要显示的属性.

在Script中检查将我带到了jquery-1.6.2.min.js中的一个位置

sis*_*onb 18

如果指定不带var的变量,则将该变量分配给全局空间.在您的情况下,有一个分配给confirm变量的字符串覆盖了本机确认方法.


sis*_*onb 16

confirm方法是window对象的一部分.

if (window.confirm(key)){
    console.log(item);
}
Run Code Online (Sandbox Code Playgroud)

  • @sissonb当我将 console.log(confirm) 放入循环中时,它打印了我的错误消息之一。经过进一步检查,发现我在将消息放入消息对象之前使用了一个名为“confirm”的变量来存储消息。如果您写了一个关于没有名为“confirm”的变量的答案,我会给您信用! (2认同)