我的 indexOf() 不工作

My indexOf() is not working

我项目的 link 在这里: http://codepen.io/xruizify/pen/aJKpNJ

var reservedTimes = [];

$(function() {                       
   var idx = reservedTimes.indexOf(currentSelection);
   $("td").click(function() { 
      var selectedTime = $(this).parent().prop('className');
      var weekDay = $(this).prop('className').split(" ")[0];
      var currentSelection = weekDay + "and" + selectedTime;
      //var idx = reservedItems.indexOf(currentSelection); It didn't work here
      if ( $(this).is(".reserved") ) {  

      } else if($(this).is(".active")) {
        $(this).removeClass("active"); 
        //var idx = reservedItems.indexOf(currentSelection); I get 'undefined'
        document.getElementById("currentIDX").innerHTML = idx;   
        //The comment below is what I want to do in the future once idx works
        //reservedTimes.splice(idx,1);
        printTimes();      
      } else{
        $(this).addClass("active"); 
        document.getElementById("mainHeader").innerHTML = selectedTime;      
        document.getElementById("currentSelection").innerHTML = currentSelection; 
        document.getElementById("currentIDX").innerHTML = idx;
        document.getElementById("secondHeader").innerHTML = weekDay;
        reservedTimes.push(currentSelection);
        printTimes(); 
   } 
 });
});
function makeReservation() {
  $(".active").addClass("reserved");
  $(".active").removeClass("active");
  printTimes();
 }
 function printTimes() {
   document.getElementById("timeTitle").innerHTML = reservedTimes;
 }

基本上,您单击 table 中的一个单元格,它会生成一个特殊的 "code" 并将其添加到数组中。 table 中的每个单元格始终生成相同的唯一代码,并且不会更改。我希望能够接受它,在数组中获取它的 indexOf() 值,并在第二次单击它时将其删除。无论我在哪里声明 idx 变量,它都不起作用,我无法弄清楚这个。

它不起作用,因为您试图在不存在的 reservedItems 上调用 indexOf;我认为你的意思是 reservedTimes 这是你要推送的数组,然后你可以按原样调用 splice

这在您的代码笔中按预期工作(已修复 http://codepen.io/anon/pen/XMYpxa):

var reservedTimes = [];

$(function() {                          
  $("td").click(function() { 
    var selectedTime = $(this).parent().prop('className');
    var weekDay = $(this).prop('className').split(" ")[0];
    var currentSelection = weekDay + "and" + selectedTime;
    var idx = reservedTimes.indexOf(currentSelection); 
    if ( $(this).is(".reserved") ) {  

    } else if($(this).is(".active")) {
      $(this).removeClass("active"); 
      document.getElementById("currentIDX").innerHTML = idx;
      reservedTimes.splice(idx,1);
      printTimes();      
    } else{
      $(this).addClass("active"); 
      document.getElementById("mainHeader").innerHTML = selectedTime;      
      document.getElementById("currentSelection").innerHTML = currentSelection; 
      document.getElementById("currentIDX").innerHTML = idx;
      document.getElementById("secondHeader").innerHTML = weekDay;
      reservedTimes.push(currentSelection);
      printTimes(); 
    } 
  });

});
function makeReservation() {
    $(".active").addClass("reserved");
    $(".active").removeClass("active");
    printTimes();
}
function printTimes(){
  document.getElementById("timeTitle").innerHTML = reservedTimes;
}