如何在 Javascript 中启动有关文本字段正确答案的弹出窗口
How to launch a pop up on the correct answering of a text field in Javascript
我正在创建一个在线游戏,玩家必须在关闭弹出窗口时回答问题。如果他们回答错误,他们会收到一个警报,告诉他们是否回答错误。如果他们回答正确,他们会收到下一个问题的弹出窗口。我的功能不正确,但如果正确的位正常工作,我无法弹出。我做错了什么?请注意,我对Javascript很陌生,所以这几乎完全是其他人的代码。
function Start1(URL1, WIDTH, HEIGHT) {
windowprops = "left=50,top=50,width=" + WIDTH + ",height=" + HEIGHT;
preview = window.open(URL1, "", windowprops);
}
function doPopup1() {
url1 = "focus.html";
width = 500; // width of window in pixels
height = 500; // height of window in pixels
delay = 5; // time in seconds before popup opens
timer = setInterval("Start1(url1, width, height)", delay*1000);
}
function doPopup2() {
url2 = "q1.html";
width = 500; // width of window in pixels
height = 500; // height of window in pixels
delay = 1; // time in seconds before popup opens
timer = setTimeout("Start1(url2, width, height)", delay*1000);
}
function checkAnswer (form){
var Answer = form.Answer.value;
if (Answer == "2"){
doPopup2();
}
else{
alert ("Incorrect")
}
}
<body OnLoad="doPopup1();">
然后是一个基本的HTML表格,但重要的部分是这样的:
<input type="submit" name="Submit" value="Submit" onClick="checkAnswer(this.form)" />
我根据你上一条评论简化了你的代码,并尝试了这个:
function Start1(URL1, WIDTH, HEIGHT) {
console.log('Start1 called:' + URL1);
}
function doPopup2() {
url2 = "q1.html";
width = 500; // width of window in pixels
height = 500; // height of window in pixels
delay = 1; // time in seconds before popup opens
timer = setTimeout("Start1(url2, width, height)", delay * 1000);
}
doPopup2();
在我的开发工具中,这会导致错误Start1 is not defined.
记录到控制台。一般来说,用字符串调用 setTimeout 是一个非常糟糕的主意。您需要将该字符串替换为函数。我们将使用匿名函数,以便我们可以传入适当的参数。
function Start1(URL1, WIDTH, HEIGHT) {
console.log('Start1 called:' + URL1);
}
function doPopup2() {
url2 = "q1.html";
width = 500; // width of window in pixels
height = 500; // height of window in pixels
delay = 1; // time in seconds before popup opens
timer = setTimeout(function() { Start1(url2, width, height); }, delay * 1000);
}
doPopup2();
再清理一下,这应该对您有用。确保使用 var
声明变量,否则它们将成为全局变量。我猜这就是为什么你在某些地方有 url1 和 url2 的原因。
function showPopup(url, width, height) {
var windowprops = "left=50,top=50,width=" + width + ",height=" + height;
return window.open(url, "", windowprops);
}
function doPopup1() {
var url = "focus.html";
var width = 500; // width of window in pixels
var height = 500; // height of window in pixels
var delay = 5; // time in seconds before popup opens
return setTimeout(function() {
showPopup(url, width, height);
}, delay * 1000);
}
function doPopup2() {
var url = "q1.html";
var width = 500; // width of window in pixels
var height = 500; // height of window in pixels
var delay = 1; // time in seconds before popup opens
return setTimeout(function() {
showPopup(url, width, height);
}, delay * 1000);
}
function checkAnswer(form) {
var answer = form.Answer.value;
if (answer == "2") {
doPopup2();
}
else {
alert("Incorrect")
}
}
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 防止Iframe窗体在新窗口中打开
- 内部分区字体大小获胜'调整浏览器窗口大小时不会随媒体查询而更改
- 调整窗口大小时,可拖动的对象会出现在容器外部
- Javascript排序的图像弹出窗口..可以't单独弹出
- 窗口大小html css
- 弹出窗口出现,然后退出
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- Javascript更新孙窗口中的表单元素
- 如何在选项卡上定义属性'的主窗口对象
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- 召回窗口加载事件 - javascript
- 为什么不显示警报窗口
- 如何使用modalDialog来显示可以在所有浏览器中工作的弹出窗口
- Chrome应用程序调整窗口大小保持纵横比
- 新选项卡被弹出窗口阻止程序阻止
- 如何访问UIWebView'的子窗口上下文
- 为什么不'我的窗口滚动事件根本没有启动.其他答案没有解决问题
- 带有多个答案选项的Javascript动态数组窗口
- 如何在 Javascript 中启动有关文本字段正确答案的弹出窗口