Javascript - 在 jquery .click() 函数中确认()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8085760/
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 - confirm() inside a jquery .click() function
提问by Mechaflash
I have the following
我有以下
$("element").click(function() {
var foo=bar;
if ( foo == "bar" ) {
confirm('Dialogue');
}
});
But I would like to bool the confirm function. I've already tried
但我想布尔确认功能。我已经试过了
$("element").click(function() {
var foo=bar;
if ( foo == "bar" ) {
var confirm=confirm('Dialogue');
if (confirm==true) {
alert('true');
} else {
alert('false');
}
}
});
But no confirmation box is generated. How can I accomplish this?
但是没有生成确认框。我怎样才能做到这一点?
回答by epascarello
You have a few issues. First issue is you are defining a variable with the name confirm
. Not good! Rename it to isGood
or something else.
你有几个问题。第一个问题是您正在定义一个名称为 的变量confirm
。不好!将其重命名为isGood
或其他名称。
The other bug is right here:
另一个错误就在这里:
if (confirm=true) {
confirm=true
is assignment, not a comparison.
confirm=true
是赋值,不是比较。
It needs to be
它需要是
if (confirm==true) {
or just
要不就
if (confirm) {
So your code would be something like
所以你的代码会是这样的
var bar = "bar";
$("button").click(function() {
var foo=bar;
if ( foo == "bar" ) {
var isGood=confirm('Dialogue');
if (isGood) {
alert('true');
} else {
alert('false');
}
}
});
回答by Fatima Zohra
if( !confirm('Are you sure you want to continue?')) {
return false;
}
回答by Dennis
When you declare a variable anywhere in your function, it automatically gets "pulled" to the top as a local variable. When you call confirm as a function, it finds the local variable first (which hasn't been defined yet) and doesn't go up the scope chain to window
where the function lives.
当你在函数的任何地方声明一个变量时,它会自动作为局部变量“拉”到顶部。当您将 confirm 作为函数调用时,它首先找到局部变量(尚未定义)并且不会沿着作用域链向上window
到达函数所在的位置。
$("element").click(function() {
var foo=bar;
if ( foo == "bar" ) {
var confirm=confirm('Dialogue');
if (confirm==true) {
alert('true');
} else {
alert('false');
}
}
});
is the same as
是相同的
$("element").click(function() {
var foo=bar, confirm = undefined;
if ( foo == "bar" ) {
confirm=confirm('Dialogue');
...
});
You could 1) rename your variable, 2) call window.confirm("Dialog")
telling it you want the global function instead of the local variable or 3) just put the confirm call inside an if
您可以 1) 重命名您的变量,2) 调用window.confirm("Dialog")
告诉它您想要全局函数而不是局部变量或 3) 只需将确认调用放在 if
$("element").click(function() {
var foo=bar;
if ( foo == "bar" ) {
if (confirm('Dialogue')) {
alert('true');
} else {
alert('false');
}
}
});
回答by BNL
All of the comments about the comparison are correct, however, the reason the confirm dialog is not showing is that you are wiping out the confirm box object.
关于比较的所有评论都是正确的,但是,确认对话框未显示的原因是您正在清除确认框对象。
Change the name of the confirm
var.
更改confirm
var的名称。
$(element).click(function() {
var confirm1 = confirm('Dialogue');
if (confirm1) {
alert('true');
} else {
alert('false');
}
});
Compare that to this one which doesn't work.
将它与这个不起作用的相比。
$(element).click(function() {
var confirm = confirm('Dialogue');
if (confirm) {
alert('true');
} else {
alert('false');
}
});
回答by Rafay
try
尝试
if (confirm) {
alert('true');
} else {
alert('false');
}
回答by John Hartsock
My initial though is your main problem lies here
我最初的问题是你的主要问题在这里
var foo=bar;
if ( foo == "bar" ) {
what is bar? I believe you intended to make the following assignment var foo="bar";
which would make the if statement evaluate as true and execute your confirmation dialog.
什么是酒吧?我相信您打算进行以下分配var foo="bar";
,这将使 if 语句评估为真并执行您的确认对话框。
In addition the following is inaccurate
另外以下说法不准确
var confirm = confirm("dialog");
if (confirm=true)
just use this:
只需使用这个:
if (confirm("dialog"))
回答by Salam
Add .confirm
class to an element then in jQuery write..
将.confirm
类添加到元素然后在 jQuery 中写入..
$('.confirm').click(function() {
return confirm("Are you sure?");
});