-2

I just started learning Javascript, so maybe this is doing exactly what it's supposed to be doing, but this is what I think is a problem.

I have a text box and submit button. I have a <p> of text that is hidden.

IF the user's text entered is what I want it to be, the <p> of text shows on submit.

ELSE (any other user's text), the text shows up, but

document.getElementById("id").innerHTML="Incorrect!";
document.getElementById("id").style.display="block;"

So, it works that if you enter the CORRECT word first, the <p> shows up.

If INCORRECT, it shows up, but with different text, ie, "incorrect!" So far, so good...

THE PROBLEM: After you get the text wrong, you can't try again, you have to refresh. If you get it right, the right text shows, if you enter something else in after, the proper <p> follows, too, but if you try AFTER the wrong text is entered, it doesn't go back.

So once it's incorrect, you can't go back. So if someone got it wrong, and then put in the right word, they wouldn't be able to tell unless they refreshed and tried it again first.... Does this make sense? I hope I'm clear.

4

1 回答 1

0

你没有发布太多代码,所以我会在这个答案中做出一些假设,我希望很清楚

假设你有我要调用的“文本框”<input type="text" id="textbox"/>和我要调用的box消息区<p id="id">sometext</p>msg

var box = document.getElementById('textbox'),
    msg = document.getElementById('id');

然后在处理提交的代码中,说

if (box.value === 'foo') { // contains the right thing
    // ...
} else { // contains the wrong thing
    msg.innerHTML = 'Incorrect!';
    msg.style.display = 'block';
}

您说第一次效果很好,但第二次不行,那么您需要考虑这两次调用之间实际发生了什么变化?也许您应该在 之前重新设置默认值if,例如我猜您的消息开始隐藏,因此您可以在该if行之前添加以下内容以确保您从预期的位置开始

msg.style.display = 'none';
于 2013-11-07T02:34:29.940 回答