如何在 Javascript 中启动有关文本字段正确答案的弹出窗口

How to launch a pop up on the correct answering of a text field in Javascript

本文关键字:答案 窗口 字段 文本 Javascript 启动      更新时间:2023-09-26

我正在创建一个在线游戏,玩家必须在关闭弹出窗口时回答问题。如果他们回答错误,他们会收到一个警报,告诉他们是否回答错误。如果他们回答正确,他们会收到下一个问题的弹出窗口。我的功能不正确,但如果正确的位正常工作,我无法弹出。我做错了什么?请注意,我对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")
    }
}​