Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

javascript - JS - Check if the input field equals the solution

I'm making a simple website for little kids with mathproblems,

the page contains a number of questions that the kids need to solve. They can press on 1 button that checks if the value that they inputted, equal is to the solution. JAVASCRIPT is obligated

I have this in HTML:

Question 1: how much is 8 x 4? 
<br><input type="text"> <br>
<button onclick="question1()">Check if your answer is correct?</button> 

my function already has this, but i can't seem to make up my mind with the syntax that looks correct:

function question1() {
  if (value == 32) {
     window.alert("correct!");
  } else {
     window.alert("try again);
  }
}

but my syntax looks correct?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to give an input box an unique ID. Then, you can obtain its value like this:

var value = document.getElementById("your-id").value;

Full code: http://jsfiddle.net/7uwNn/

Good luck with your project!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...