简体   繁体   English

如何在javascript中将消息附加/更新到单个警报消息?

[英]How do I append/update a message to a single alert message in javascript?

I'm trying to print the entire results at once onto a single alert message. 我正在尝试将整个结果一次打印到单个警报消息上。 I tried to put it in with the same variable answerText where everything prints but it doesn't work. 我试图用相同的变量answerText将其放入其中,但是它不起作用。 I tried adding alert to all of them and it pops up separately. 我尝试向所有人添加警报,然后单独弹出。 Anyone knows how to put all results into a single alert message, not all over the place? 任何人都知道如何将所有结果放入单个警报消息,而不是所有地方? Right now, the code doesn't show any alert boxes. 现在,代码不显示任何警告框。 Thanks! 谢谢!

JavaScript: JavaScript的:

var ans = new Array;
var done = new Array;
var yourAns = new Array;
//var explainAnswer = new Array;

var score = 0;
ans[1] = "D";
ans[2] = "A";
ans[3] = "D";
ans[4] = "A";
ans[5] = "B";

function Engine(question, answer) 
{
yourAns[question] = answer;
}

function Score()
{
alert("TESTING!");
var answerText = "How did you do?\n------------------------------------\n";
for(i = 1; i <= 5; i++)
{
    answerText = answerText + "\nQuestion :" + i + "\n";
    if(ans[i] != yourAns[i])
    {
        answerText = answerText + "\nThe correct answer was " + ans[i];
    }
    else
    {
        answerText = answerText + " \nCorrect! \n";
        score++;
    }
}
answerText = answerText + "\n\nYour total score is : " + score + "\n";
 }

try 尝试

 function Score()
    {
    alert("TESTING!");
    var answerText = "How did you do?\n------------------------------------\n";
    for(i = 1; i <= 5; i++)
    {
        answerText = answerText + "\nQuestion :" + i + "\n";
        if(ans[i] != yourAns[i])
        {
            answerText = answerText + "\nThe correct answer was " + ans[i];
        }
        else
        {
            answerText = answerText + " \nCorrect! \n";
            score++;
        }
    }
    answerText = answerText + "\n\nYour total score is : " + score + "\n";
    alert(answerText );

 }

There are two issues with your code 您的代码有两个问题

  • You declared a function Score but never called it. 您声明了一个函数Score但从未调用它。
  • answerText is created but you never passed its value to alert. answerText已创建但您从未将其值传递给警报。


var ans = new Array;
var done = new Array;
var yourAns = new Array;
//var explainAnswer = new Array;

var score = 0;
ans[1] = "D";
ans[2] = "A";
ans[3] = "D";
ans[4] = "A";
ans[5] = "B";

function Engine(question, answer) 
{
yourAns[question] = answer;
}

function Score()
{
alert("TESTING!");
var answerText = "How did you do?\n------------------------------------\n";
for(i = 1; i <= 5; i++)
{
    answerText = answerText + "\nQuestion :" + i + "\n";
    if(ans[i] != yourAns[i])
    {
        answerText = answerText + "\nThe correct answer was " + ans[i];
    }
    else
    {
        answerText = answerText + " \nCorrect! \n";
        score++;
    }
}
answerText = answerText + "\n\nYour total score is : " + score + "\n";
    // shows an alert
    alert(answerText);
 }
// call a Score function
Score();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中的单个警报消息中显示表单值 - How to display a form values within a single alert message in javascript 如何使用javascript阅读警报消息? - How to read alert message with javascript? 单个警报消息javascript中的多种形式验证错误消息 - multiple form validation error message in single alert message javascript 如何在javascript警报消息启动时重复播放声音,并在警报消息解除时停止播放? - How can I repeatedly play a sound while a javascript alert message is up and stop it when the alert message is dismissed? 如何使用alert()获取长调试消息? - How do I use alert() for a long debug message? 如何从警报消息更改为常规文本 output? - How do I change from an alert message to a regular text output? 如何在OfficeJS中显示类似于Window.alert()的消息 - How do I display a message similar to Window.alert() in OfficeJS jQuery-如何向该jQuery代码库添加警报消息? - jquery - How do I add an alert message to this jquery code base? 有一个带有链接Java脚本的HTML文档,当我单击提交按钮时,我试图添加一条警报消息。 我该怎么做呢? - have an HTML document with a linked Javascript I am trying to add an alert message when a submit button is clicked. How do I do this? 模态消息作为javascript警报? - Modal message as javascript alert?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM