简体   繁体   English

用户尝试回答后,如何重置游戏板以进行下一个选择(jQuery)

[英]After user attempts answer, how to reset gameboard for next selection (jquery)

I'm doing a jeopardy game. 我在玩危险游戏。 I am able to have a user select a square, the question appears with an input box, they click submit and the answer is compared to the correct answer. 我能够让用户选择一个正方形,问题出现并带有输入框,他们单击“提交”,然后将答案与正确答案进行比较。 If correct, say so and add points to total, if wrong, say so and deduct points from total. 如果正确,则说出并在总分中加分;如果错误,则说出并从总分中扣除。 But then the game is stuck. 但随后游戏停滞了。 I can't figure out how to have the game be ready for the next question to be selected. 我无法弄清楚如何为选择下一个问题做好准备。

Here is the js code for the first question. 这是第一个问题的js代码。 Please let me know if you want more code (I didn't want this question to be messy). 如果您需要更多代码,请让我知道(我不希望这个问题很混乱)。 There may be a simple solution, so if you're wondering "why don't you just use ______, duh" the answer is because I'm a noob and I didn't know how to. 可能有一个简单的解决方案,所以如果您想知道“为什么不只使用______ ,,”,答案是因为我是菜鸟,而且我不知道该怎么做。 Thank you so much in advance. 提前非常感谢您。

codepen: http://codepen.io/Nplagma/pen/XNerRw codepen: http ://codepen.io/Nplagma/pen/XNerRw

$(document).ready(function() {
  $(".question").hide();

var score = 0;

$(".well").hover(function() {
$(this).addClass('blue');
}, function() {
$(this).removeClass('blue');
});

$("#A1").click(function() {
var questionValue = $(this).data("questionvalue");
             $(".question").show();
             $('<h4>The \"MVP\" quarterback whose team is 14-6 when he doesn’t play.</h4>').appendTo('.question');
             $('#submit').click(function() {
                 $("#answer").on('input')
                    var answer = $('#answer').val(); //what does this mean in words? 


                    if 
                        (answer == "Tom Brady" || answer == "Brady" || answer == "brady" || answer == "tom brady") {
                        $('.question').replaceWith('<h3>omg you\'re so smart</h3>')      //using h3 because it'll be unique, but there must be a better way
                        score += questionValue;
                        $(".score").text("$" + score);
                    }
                    else 
                        {
                        $('.question').replaceWith('<h3>could NOT have been more wrong</h3>');
                        score -= questionValue;
                        $(".score").text("$" + score);
         }

});
                        });

Just for your information. 仅供参考。 The approach you have chosen isn't maybe the best one for the task. 您选择的方法可能不是完成任务的最佳方法。 your users could easily just check the code to find the answer to your questions. 您的用户可以轻松地检查代码以找到问题的答案。 A "better" approach would be to store all the data (ie questions and answers) in a database and access that with ajax. 一种“更好”的方法是将所有数据(即问题和答案)存储在数据库中,并使用ajax访问该数据。 You would then also need to learn a backend language and SQL. 然后,您还需要学习后端语言和SQL。

You are also using different states. 您还使用了不同的状态。 When this gets more complicated, you will find that its a pain to control them and keeping track of everything. 当这变得更加复杂时,您会发现控制它们并跟踪所有内容很痛苦。 This is where frontend frameworks really shines. 这就是前端框架真正发挥作用的地方。 I would recommend you to check them out. 我建议您检查一下。 There are loads of them nowadays; 如今有很多。 React, Angular, Meteor, Vue, Backbone etc. Check them out and pick one that makes most sense to you. React,Angular,Meteor,Vue,Backbone等。将它们检出并选择对您最有意义的一种。 (

However, if your only making this application to learn javascript better. 但是,如果您仅使此应用程序学习javascript更好。 Go ahead with your approach :) 继续您的方法:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM