window.alert并确认工作,但不提示(window.alert and confirm work, but not prompt)

编程入门 行业动态 更新时间:2024-10-27 01:36:29
window.alert并确认工作,但不提示(window.alert and confirm work, but not prompt)

我正在尝试显示一个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); }

运行时,输入if语句。 第一个警报显示“是”。 但是,它会跳过提示行。 下一个警报显示“未定义”。

现在,如果我唯一改变的是关键字提示,它将起作用,如下:

var is_expired = document.getElementById("is_expired").value; if (is_expired == "yes") { alert(is_expired); var answer = confirm("Are you sure you?"); alert(answer); }

运行时,会出现确认框。 如果我单击Okay,则下一个警报显示“true”,如果单击Cancel,则警告显示“false”。

所以,最重要的是,没有语法错误导致Javascript停止。 它实际上正在跳过这一行并继续执行。 这条线有什么问题? 我检查了多个网站以确保它是正确的。 添加第二个“默认”参数也无济于事。 我试过了。 此外,没有Javascript错误来指示问题。

我去了这里并把它改成了我的代码(我硬编码is_expired是的),它在那里工作。

任何帮助都会很棒。 谢谢。

编辑:要清楚,我不是依赖W3school的例子来准确,我用他们的“试一试”页面来测试我自己的代码。 我也在jfiddle做了这个,它工作得很好。 使用控制台检查函数返回“undefined”。

EDIT2:实际上,刮擦那个。 当控制台中没有命令时,我不小心再次按回车。 提示的实际输出是:

[12:07:55.940] [object Function]

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:

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".

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.

I went here and changed it to my code (I hardcoded is_expired to be yes), and it works there.

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".

EDIT2: Actually, scratch that. I accidentally hit enter again when there was no command in the console. The actual output for prompt is:

[12:07:55.940] [object Function]

最满意答案

正如所建议的那样,代码中的某些内容已经使用function prompt(s) { window.status = s }覆盖了window.prompt方法。 不确定这是否是故意的。

您可以使用一些方法来“恢复”原始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;

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.

There are a few methods you can use to "restore" the original 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 set window.prompt = null;), though this may not work in all browsers.

You can create an iframe (which creates a new window environment), and then "steal" the prompt from there.

var i = document.createElement('iframe'); i.style.display = 'none'; document.body.appendChild(i); window.prompt = i.contentWindow.prompt;

更多推荐

本文发布于:2023-07-26 03:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1270745.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但不   提示   工作   alert   window

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!