jQuery JavaScript 错误提示“确认不是函数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8059818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript error says "confirm is not a function"
提问by Keyslinger
I'm using jQuery with the validation plugin when I submit a form:
当我提交表单时,我将 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();
}
});
In the submitHandler,
在 submitHandler 中,
function SaveProject(){
//... load variables with input contents
$.ajax({
url: 'ajax/save_project.php',
dataType: 'json',
data: 'id='+id+'&title='+title+'&project='+project+'§or='+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);
}
});
}
});
}
When I submit the validated form and it gets to the confirm inside the each loop, I get the error message: "confirm is not a function".
当我提交经过验证的表单并进入每个循环内的确认时,我收到错误消息:“确认不是函数”。
How can I present a message to the user for confirmation?
如何向用户显示消息以供确认?
Edit:
编辑:
When I type "confirm" into the console I get this:
当我在控制台中输入“确认”时,我得到了这个:
Inspecting in DOM reveals:
在 DOM 中检查显示:
window > confirm() There are no properties to show for this object.
window > confirm() 此对象没有要显示的属性。
Inspecting in Script takes me to a place in jquery-1.6.2.min.js
在脚本中检查将我带到 jquery-1.6.2.min.js 中的一个位置
回答by sissonb
If you assign a variable without var, you are assigning that variable to the global space. In your case there was a string assigned to the confirm variable that overrode the native confirm method.
如果你分配一个没有 var 的变量,你就是在将该变量分配给全局空间。在您的情况下,有一个字符串分配给了确认变量,该变量覆盖了本机确认方法。
回答by sissonb
The confirm method is part on the window object.
确认方法是窗口对象的一部分。
if (window.confirm(key)){
console.log(item);
}
回答by YanXing Ou
I use both 'jquery confirm' and 'datatables', in my case, 'datatables' should be imported before 'jquery confirm'.
我同时使用'jquery confirm'和'datatables',在我的情况下,'datatables'应该在'jquery confirm'之前导入。
So, in some case, try ReOrder ........
因此,在某些情况下,请尝试重新订购........
回答by Hink
Defining any html object with attribute id ="confirm" also causes this error
定义任何具有属性 id ="confirm" 的 html 对象也会导致此错误