2015-02-24 31 views
1

我想提出一个小的文字游戏,那里有一所缺少的单词,如果你在输入栏填写正确答案它变绿缺少文字游戏(填隙)

我想一个功能添加到这个代码,但我不知道我怎么 要那么如果你把一个错误的答案则会变成红色

它只是增加了你的分数,如果你把正确的答案变绿分钟进行编辑

我知道答案是在js文件结束时将它变成绿色,如果正确

这是HTML

 <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'> </script> 
      <script type="text/javascript"> 
      $(document).ready(function(){ 
      $("input").not($(":button")).keypress(function (evt) { 
      if (evt.keyCode == 13) { 
       iname = $(this).val(); 
       if (iname !== 'Submit'){ 
       var fields = $(this).parents('form:eq(0),body').find('button,input,textarea,select'); 
       var index = fields.index(this); 
       if (index > -1 && (index + 1) < fields.length) { 
       fields.eq(index + 1).focus(); 
       } 
       return false 
      } 
      } 
     }); 
      });</script> 


     <script type="text/javascript" src="prolinguis-gap-filler.js"></script> 

<div style="font-family:Helvetica; font-size: 18px; vertical-align:bottom; color:#3e2e41; margin:16px 0;"> Score: <label id="label_score">0%</label></div> 

       <form class="game-form" style="padding: 0px; width: 100%; margin: 10px auto;" > 

        <!-- //////////////////////////////////////////////////////////////////////////////////////// --> 

        <div class="game-hold" style=" width: 100%; text-align: left; display: inline-block;"> 

        <p style="font-size: 14px; font-weight: bold; margin: 5px 0; color: #333333;">Question 1</p> 
        <p>Where is Stephen from? <input TYPE="text" id="ctr1" onBlur="validate(this.value, this.id)" style="text-transform: uppercase; font-family:Helvetica; font-size: 10px; width:150px; height:18px; text-align: left; color:#000000;"></input></p> 
        </div> 

        <!-- //////////////////////////////////////////////////////////////////////////////////////// --> 

        <div class="game-hold" style=" width: 100%; text-align: left; display: inline-block;"> 

        <p style="font-size: 14px; font-weight: bold; margin: 5px 0; color: #333333;">Question 2</p> 
        <p>If someone asks you, what’s cooking? You shouldn’t answer with: <input TYPE="text" id="ctr2" onBlur="validate(this.value, this.id)" style="text-transform: uppercase; font-family:Helvetica; font-size: 10px; width:150px; height:18px; text-align: left; color:#000000;"></input></p> 
        </div> 

        <!-- //////////////////////////////////////////////////////////////////////////////////////// --> 

        <div class="game-hold" style=" width: 100%; text-align: left; display: inline-block;"> 

        <p style="font-size: 14px; font-weight: bold; margin: 5px 0; color: #333333;">Question 3</p> 
        <p>Instead, just say <input TYPE="text" id="ctr3" onBlur="validate(this.value, this.id)" style="text-transform: uppercase; font-family:Helvetica; font-size: 10px; width:150px; height:18px; text-align: left; color:#000000;"></input></p> 
        </div> 

       </form> 

,并在js文件我有这个

<script> 
      var ctr = 0 
      var score_ctr = 0 
      function validate(value, id) { 
      if (id =='ctr1' && (value.toUpperCase()=="UNITED STATES" || value.toUpperCase()=="USA" || value.toUpperCase()=="AMERICA")) { 
       ctr = ctr + 1; 
       correct_answer(id); 
       document.getElementById(id).value="UNITED STATES"; 
      } 
      if (id =='ctr2' && (value.toUpperCase()=="GOOD")) { 
       ctr = ctr + 1; 
       correct_answer(id); 
       document.getElementById(id).value="GOOD"; 
      } 
      if (id =='ctr3' && (value.toUpperCase()=="NOTHING MUCH" || value.toUpperCase()=="NOT MUCH")) { 
       ctr = ctr + 1; 
       correct_answer(id); 
       document.getElementById(id).value="NOTHING MUCH"; 
      } 


      } 
      function correct_answer (id) { 
      score_ctr = (ctr * 100)/3 
      document.getElementById('label_score').innerHTML = score_ctr.toFixed(0) + '%' 
      document.getElementById(id).disabled=true; 
      document.getElementById(id).style.backgroundColor = '#c1d82f' 
      document.getElementById(id).style.cursor="default" 

      } 
    </script> 
+0

试着做一个jsfiddle,可以帮助那些想要帮助你的人:) – 2015-02-24 15:28:29

+0

好酷一秒生病让它成为一体 - 谢谢 – emoot 2015-02-24 15:30:11

回答

1

更改validate(value, id)以下几点:

function validate(value, id) { 
    if (id == 'ctr1' && (value.toUpperCase() == "UNITED STATES" || value.toUpperCase() == "USA" || value.toUpperCase() == "AMERICA")) { 
     ctr = ctr + 1; 
     correct_answer(id); 
     document.getElementById(id).value = "UNITED STATES"; 
    } 
    else if (id == 'ctr2' && (value.toUpperCase() == "GOOD")) { 
     ctr = ctr + 1; 
     correct_answer(id); 
     document.getElementById(id).value = "GOOD"; 
    } 
    else if (id == 'ctr3' && (value.toUpperCase() == "NOTHING MUCH" || value.toUpperCase() == "NOT MUCH")) { 
     ctr = ctr + 1; 
     correct_answer(id); 
     document.getElementById(id).value = "NOTHING MUCH"; 
    } 
    else 
    { 
     document.getElementById(id).style.backgroundColor = '#ff0000'; 
     document.getElementById(id).style.cursor = "default"; 
    } 

这将检查并核对所有不同的输入字段,如果找不到正确的答案,则会将最后模糊字段的背景设置为红色。

只是一个提示,如果你想清理一下你的代码,考虑使用switch语句来确定你正在检查哪个id

+0

感谢您的帮助 - 它完美的工作 – emoot 2015-02-24 15:46:24