javascript window.alert 并确认工作,但不提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18701990/
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
window.alert and confirm work, but not prompt
提问by Troncoso
I'm trying to display a Javascript prompt for the user to enter data (as prompts are used for). But, it won't show. window.alert shows, and so does window.confirm. But, not prompt. Here's the code I'm trying to run:
我正在尝试为用户显示一个 Javascript 提示以输入数据(如使用提示一样)。但是,它不会显示。window.alert 显示,window.confirm 也显示。但是,不提示。这是我试图运行的代码:
var is_expired = document.getElementById("is_expired").value;
if (is_expired == "yes") {
alert(is_expired);
var answer = prompt("Are you sure you?");
alert(answer);
}
When this runs, the if statement is entered. The first alert displays "yes" as it should. But, then, it skips the prompt line. The next alert displays saying "Undefined".
运行时,将输入 if 语句。第一个警报应该显示“是”。但是,它会跳过提示行。下一个警报显示“未定义”。
Now, if the only thing I change is the keyword prompt, it will work, as such:
现在,如果我唯一更改的是关键字提示,它将起作用,如下所示:
var is_expired = document.getElementById("is_expired").value;
if (is_expired == "yes") {
alert(is_expired);
var answer = confirm("Are you sure you?");
alert(answer);
}
When I run this, the confirm box appears. If I click Okay, then the next alert says "true" and if I click Cancel the alert says "false".
当我运行它时,会出现确认框。如果我单击“确定”,则下一个警报会显示“真”,如果我单击“取消”,则警报会显示“假”。
So, the big takeaway here is that there isn't a syntax error that's causing the Javascript to stop. It's actually skipping over that single line and continuing execution. What's wrong with that line? I've checked multiple sites to ensure it's correct. Adding the second "default" parameter does not help either. I tried that. Also, There are no Javascript errors to indicate the problem.
因此,这里最大的收获是没有导致 Javascript 停止的语法错误。它实际上是跳过那一行并继续执行。那条线有什么问题?我检查了多个站点以确保它是正确的。添加第二个“默认”参数也无济于事。我试过了。此外,没有 Javascript 错误可以指示问题。
I went hereand changed it to my code (I hardcoded is_expired to be yes), and it works there.
我去了这里并将其更改为我的代码(我将 is_expired 硬编码为 yes),并且它在那里工作。
Any help would be fantastic. Thanks.
任何帮助都会很棒。谢谢。
EDIT: To be clear, I'm not relying on W3school's example to be accurate, I used their "try it out" page to test my own code. I also did this on jfiddle and it worked fine. Using the console to check the function returns "undefined".
编辑:明确地说,我不依赖 W3school 的示例是准确的,我使用他们的“试用”页面来测试我自己的代码。我也在 jfiddle 上做了这个,效果很好。使用控制台检查函数返回“未定义”。
EDIT2: Actually, scratch that. I accidentally hit enter again when there was no command in the console. The actual output for prompt is:
EDIT2:实际上,从头开始。当控制台中没有命令时,我不小心再次按下了 Enter 键。提示的实际输出是:
[12:07:55.940] [object Function]
采纳答案by Rocket Hazmat
As suggested, something in your code has overridden the window.prompt
method with function prompt(s) { window.status = s }
. Not sure if this was intentional or not.
正如所建议的,您的代码中的某些内容已window.prompt
使用function prompt(s) { window.status = s }
. 不确定这是故意的还是无意的。
There are a few methods you can use to "restore" the original prompt
.
您可以使用几种方法来“恢复”原始prompt
.
You can backup the original at the very start of your page:
var originalPrompt = window.prompt; // Whatever code is on your page function prompt(s) { window.status = s }
Then use
originalPrompt
instead:var answer = originalPrompt("Are you sure you?");
You can
delete window.prompt;
(or setwindow.prompt = null;
), though this may not work in all browsers.You can create an iframe (which creates a new
window
environment), and then "steal" theprompt
from there.var i = document.createElement('iframe'); i.style.display = 'none'; document.body.appendChild(i); window.prompt = i.contentWindow.prompt;
您可以在页面的最开始备份原始文件:
var originalPrompt = window.prompt; // Whatever code is on your page function prompt(s) { window.status = s }
然后
originalPrompt
改用:var answer = originalPrompt("Are you sure you?");
您可以
delete window.prompt;
(或设置window.prompt = null;
),尽管这可能不适用于所有浏览器。您可以创建一个 iframe(它创建一个新
window
环境),然后prompt
从那里“窃取” 。var i = document.createElement('iframe'); i.style.display = 'none'; document.body.appendChild(i); window.prompt = i.contentWindow.prompt;
回答by Christoph
prompt
should pretty much work in every browser. One possibility is to check if you didn't accidently override this function somewhere in your source. If you type prompt
in the browserconsole when on your site, it should state something like this:
prompt
应该几乎适用于每个浏览器。一种可能性是检查您是否没有在源代码中的某处意外覆盖此函数。如果您prompt
在您的网站上输入浏览器控制台,它应该显示如下内容:
> prompt
function prompt() { [native code] }
// or put in your code:
alert(window.promt);
Otherwise it got overridden somehow (most likely by forgetting the var
keyword):
否则它会以某种方式被覆盖(很可能是因为忘记了var
关键字):
> prompt = function(){alert("foo!")}
> prompt
function (){alert("foo")}
// calling prompt() would now pop an alert box
回答by Frambot
When declaring variables in a function, make sure you use the var
keyword so it doesn't clobber the global namespace. It sounds like you're doing this somewhere:
在函数中声明变量时,请确保使用var
关键字,以免破坏全局命名空间。听起来您正在某处执行此操作:
function my_thing() {
var i = 0;
var j = 0;
prompt = "What the heck";
}
Omitting the var
keyword puts prompt
in the global namespace, clobbering the default prompt
function.
省略var
关键字 putsprompt
在全局命名空间中,破坏默认prompt
功能。