2013-05-10 83 views
1

此問題已被編輯, 我需要重置此代碼與重置按鈕,所以我可以從currentItem 1再次使用它,問題是隻有當前項目被重置,突出顯示列表項仍然設置爲當前項目不在1處。jquery javascript reset array to 1

var stuff = new Array(); 
stuff["li_1"]="ext_1"; 
stuff["li_2"]="hc_1"; 
stuff["li_3"]="ext_2"; 
stuff["li_4"]="ext_3"; 
stuff["li_5"]="hc_2"; 
stuff["li_6"]="ext_4"; 
stuff["li_7"]="hc_3"; 
stuff["li_8"]="ext_5"; 
stuff["li_9"]="hc_4"; 
stuff["li_10"]="ext_6"; 
stuff["li_11"]="hc_5";  
var currentItem=1; 
var extWrong=false; 
var extCorrect=false; 
var hcWrong=false; 
var hcCorrect=false; 

$(document).ready(function(e) { 
    //document.ontouchmove = function(e){ e.preventDefault(); } 
    $('#li_1').draggable({revert:true}); 
    $("#li_1").addClass("highlighted"); 
    $('.drop_area').droppable({ 
     tolerance:"pointer", 
     drop:function (event, ui) { 
      var tmpID=$(ui.draggable).attr("id"); 
      var droppableID = $(this).attr("id"); 
      var audioToPlay=""; 
      $("#" + tmpID).hide();   
      $("#" + stuff[tmpID]).show(); 
      $("#" + stuff[tmpID]).addClass("highlighted"); 

      currentItem++; 

      var dragType=tmpID.split("_")[1]; 
      dragType=(dragType=="1" || dragType=="3" ||dragType=="4" ||dragType=="6" ||dragType=="8" ||dragType=="10")?"ext":"hc"; 
      var dropType=droppableID=="extreme_thoughts"?"ext":"hc"; 

      if(dragType=="ext" && dropType=="ext"){ 
        audioToPlay=extCorrect==false?"IntDragDrop_ExtremeCorrect.mp3":"ding.mp3"; 
        extCorrect=true; 
      }else if(dragType=="ext" && dropType=="hc"){ 
        audioToPlay=extWrong==false?"IntDragDrop_ExtremeIncorrect.mp3":"pop4c.mp3"; 
        extWrong=true; 
      }else if(dragType=="hc" && dropType=="hc"){ 
        audioToPlay=hcCorrect==false?"IntDragDrop_HealthyCorrect.mp3":"ding.mp3"; 
        hcCorrect=true; 
      }else if(dragType=="hc" && dropType=="ext"){ 
       audioToPlay=hcWrong==false?"IntDragDrop_HealthyIncorrect.mp3":"pop4c.mp3"; 
        hcWrong=true; 

      } 
      playSound(audioToPlay,"#" + stuff[tmpID],"#" + "li_" + currentItem); 

      } 

    }); 

這裏是我的壞按鈕。

$("#restart").click(function(){ 
    currentItem=1; 
    var extWrong=false; 
    var extCorrect=false; 
    var hcWrong=false; 
    var hcCorrect=false; 
    $("#ext_1,#ext_2,#ext_3,#ext_4,#ext_5,#ext_6,#hc_1,#hc_2,#hc_3,#hc_4,#hc_5,#congrats").hide() 
    $("#li_1,#li_2,#li_3,#li_4,#li_5,#li_6,#li_7,#li_8,#li_9,#li_10,#li_11,#hide_me,#hide_me2").show() 
    $("#li_2,#li_3,#li_4,#li_5,#li_6,#li_7,#li_8,#li_9,#li_10,#li_11").removeClass("highlighted"); 
    alert(currentItem) 
}); 
+3

'currentItem'對我來說看起來不像一個數組。 – j08691 2013-05-10 16:37:44

+0

你的第二行代碼也有'currentItem == 1;',使用'=='而不是'=',所以這行只是計算一個布爾值而沒有其他任何事情發生(沒有分配完成)。 – ajp15243 2013-05-10 16:40:00

+0

我不知道那會是什麼。它保存的值會發生變化,那麼我該如何重置var? – user2089255 2013-05-10 16:40:15

回答

2

您的分配的錯字,你正在做currentItem==1代替currentItem=1;

+0

編輯,這是我訴諸堆棧之前嘗試的東西,只是忘了把它改回一個「=」 – user2089255 2013-05-10 16:41:51