2011-10-24 90 views
0

由於我對JavaScript不太擅長,我在這段代碼中遇到了一些小問題。出於某種原因,我搞砸了語法,因爲我使用的是Visual Studio 2010,所以沒有真正的方法來調試JavaScript。我不確定我做錯了什麼,但是當我嘗試在瀏覽器中啓動這段代碼時,它似乎只是繼續加載,並沒有真正完成。我想知道我的表情是否有問題,但是無論如何,我已經在這方面做了很多工作,並且開始讓我感到沮喪。不允許頁面加載的JavaScript

任何答案將不勝感激。

var addAbsence = { 
holDayCount: 0, 

init: function() { 
    var deferrend = $.Deferred(); 

    $("#addAbsence select[name=type]").change(function() { 
     switch ($(this).val()) { 
      case "0": case "1": 
       $("#addAbsence .borderWrap.type").animate({ "width": 266 }, 300, "swing", function() { 
        $("#addAbsence .borderWrap.tInfo").show(); 
       }); 
       $("#addAbsence .halfDays").show(); 
       $("#addAbsence .lateArrivalTime").hide(); 
       $("#addAbsence .paidHolidayCount").show(); 
       break; 
      case "2": 
       $("#addAbsence .borderWrap.type").animate({ "width": 266 }, 300, "swing", function() { 
        $("#addAbsence .borderWrap.tInfo").show(); 
       }); 
       $("#addAbsence .halfDays").hide(); 
       $("#addAbsence .paidHolidayCount").hide(); 
       $("#addAbsence .lateArrivalTime").show(); 
       break; 
      default: 
       $("#addAbsence .borderWrap.tInfo").hide(); 
       $("#addAbsence .halfDays").hide(); 
       $("#addAbsence .borderWrap.type").animate({ "width": 554 }, 300, "swing"); 
     } 
    }); 

    $("#addAbsence input.date").datepicker({ 
     "dateFormat": "DD, d MM, yy", 
     onSelect: function() { 
      var dateFrom = Date.parse($("#addAbsence input[name=dateFrom]").val()); 
      var dateTo = Date.parse($("#addAbsence input[name=dateTo]").val()); 

      if (!isNaN(dateFrom) && !isNaN(dateTo)) { 
       var dates = addAbsence.getStartEndDate(); 

       if ((dateFrom <= dateTo) && 
        (dateFrom >= dates.dateFrom && dates.dateFrom <= dateTo) && 
        (dateFrom <= dates.dateTo && dates.dateTo >= dateTo)) { 

        $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").removeClass("error"); 

        if ($("#addAbsence select[name=type]").val() == "0") { 
         addAbsence.calcDayCount(); 

         $("#addAbsence p.holUsing").html(addAbsence.holDayCount); 
        } 
        else { 
         $("#addAbsence p.holUsing").html("0"); 
        } 
       } 
       else { 
        $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").addClass("error"); 

        $("#addAbsence p.holUsing").html("--"); 
       } 
      } 
      else { 
       $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").removeClass("error"); 
      } 
     } 
    }); 

    $("#addAbsence .addHalfDay").click(function() { 
     addAbsence.calcDayCount(); 

     if ($("#addAbsence input[name=dateFrom]").val().length > 0 && $("#addAbsence input[name=dateTo]").val().length > 0 && 
      Date.parse($("#addAbsence input[name=dateFrom]").val()) <= Date.parse($("#addAbsence input[name=dateTo]").val())) { 

      $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").removeClass("error"); 

      if ($("#addAbsence tbody tr").size() < addAbsence.holDayCount) { 
       addAbsence.addHalfDay(); 
      } 
     } 
     else { 
      $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").addClass("error"); 

    $("#addAbsence select[name=type]").change(); 
    )}; 


    $("form#addAbsence").submit(function() { 
     var boolDate = $(this).find("input[name=dateFrom]").dateRangeValid($(this).find("input[name=dateTo]")); 
     var boolComments = $(this).find("textarea[name=comments]").lengthValid(0, 350); 
     var boolAbsenceType = $(this).find("select[name=type]").selectValid(1); 

     )}; 
+0

在firebug或Chrome開發工具中打開您的頁面/代碼,看看有哪些錯誤。如果你想在此之後獲得一些幫助,你想把所有的代碼放在http://jsfiddle.net/並在這裏分享,這樣人們就可以看一看現場實例。 –

+0

事情是這樣的,它並沒有發現任何錯誤:( – kellehbeans

回答

0

您的代碼底部有兩個嵌套錯誤。我不知道這是不是。這裏是固定代碼:

$("#addAbsence select[name=type]").change(function() { 
    switch ($(this).val()) { 
     case "0": case "1": 
      $("#addAbsence .borderWrap.type").animate({ "width": 266 }, 300, "swing", function() { 
       $("#addAbsence .borderWrap.tInfo").show(); 
      }); 
      $("#addAbsence .halfDays").show(); 
      $("#addAbsence .lateArrivalTime").hide(); 
      $("#addAbsence .paidHolidayCount").show(); 
      break; 
     case "2": 
      $("#addAbsence .borderWrap.type").animate({ "width": 266 }, 300, "swing", function() { 
       $("#addAbsence .borderWrap.tInfo").show(); 
      }); 
      $("#addAbsence .halfDays").hide(); 
      $("#addAbsence .paidHolidayCount").hide(); 
      $("#addAbsence .lateArrivalTime").show(); 
      break; 
     default: 
      $("#addAbsence .borderWrap.tInfo").hide(); 
      $("#addAbsence .halfDays").hide(); 
      $("#addAbsence .borderWrap.type").animate({ "width": 554 }, 300, "swing"); 
    } 
}); 

$("#addAbsence input.date").datepicker({ 
    "dateFormat": "DD, d MM, yy", 
    onSelect: function() { 
     var dateFrom = Date.parse($("#addAbsence input[name=dateFrom]").val()); 
     var dateTo = Date.parse($("#addAbsence input[name=dateTo]").val()); 

     if (!isNaN(dateFrom) && !isNaN(dateTo)) { 
      var dates = addAbsence.getStartEndDate(); 

      if ((dateFrom <= dateTo) && 
       (dateFrom >= dates.dateFrom && dates.dateFrom <= dateTo) && 
       (dateFrom <= dates.dateTo && dates.dateTo >= dateTo)) { 

       $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").removeClass("error"); 

       if ($("#addAbsence select[name=type]").val() == "0") { 
        addAbsence.calcDayCount(); 

        $("#addAbsence p.holUsing").html(addAbsence.holDayCount); 
       } 
       else { 
        $("#addAbsence p.holUsing").html("0"); 
       } 
      } 
      else { 
       $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").addClass("error"); 

       $("#addAbsence p.holUsing").html("--"); 
      } 
     } 
     else { 
      $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").removeClass("error"); 
     } 
    } 
}); 

$("#addAbsence .addHalfDay").click(function() { 
    addAbsence.calcDayCount(); 

    if ($("#addAbsence input[name=dateFrom]").val().length > 0 && $("#addAbsence input[name=dateTo]").val().length > 0 && 
     Date.parse($("#addAbsence input[name=dateFrom]").val()) <= Date.parse($("#addAbsence input[name=dateTo]").val())) { 

     $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").removeClass("error"); 

     if ($("#addAbsence tbody tr").size() < addAbsence.holDayCount) { 
      addAbsence.addHalfDay(); 
     } 
    } 
    else { 
     $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").addClass("error"); 
    } 

    $("#addAbsence select[name=type]").change(); 
}); 


$("form#addAbsence").submit(function() { 
    var boolDate = $(this).find("input[name=dateFrom]").dateRangeValid($(this).find("input[name=dateTo]")); 
    var boolComments = $(this).find("textarea[name=comments]").lengthValid(0, 350); 
    var boolAbsenceType = $(this).find("select[name=type]").selectValid(1); 
}); 
+0

謝謝你,抱歉,我花了很長時間回覆,但你是對的,我也有其他人在我的公司看到它,發現這是問題。謝謝。:-) – kellehbeans

+0

沒問題。很高興有幫助。 –

0

visual studio應該向你顯示標記錯誤。 我使用的方法之一是刪除幾段代碼並嘗試運行。一旦你對20-30行代碼進行調零,就會更容易找到這些錯誤。 然而在這種情況下,我用jsLint http://www.jslint.com/ ,它想出了Problem at line 84 character 5: Expected an identifier and instead saw ')'. ,然後與支架匹配的記事本++看到$("#addAbsence .addHalfDay").click(function() {包含一些錯位的字符。 之後發現更多的語法錯誤與jslint。在末尾替換以下標記

//no errors before this 
    $("#addAbsence .addHalfDay").click(function() { 
    addAbsence.calcDayCount(); 

    if ($("#addAbsence input[name=dateFrom]").val().length > 0 && $("#addAbsence input[name=dateTo]").val().length > 0 && 
     Date.parse($("#addAbsence input[name=dateFrom]").val()) <= Date.parse($("#addAbsence input[name=dateTo]").val())) { 

     $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").removeClass("error"); 

     if ($("#addAbsence tbody tr").size() < addAbsence.holDayCount) { 
      addAbsence.addHalfDay(); 
     } 
    } 
    else { 
     $("#addAbsence input[name=dateFrom], #addAbsence input[name=dateTo]").addClass("error"); 
    } 
$("#addAbsence select[name=type]").change(); 
}); 


$("form#addAbsence").submit(function() { 
    var boolDate = $(this).find("input[name=dateFrom]").dateRangeValid($(this).find("input[name=dateTo]")); 
    var boolComments = $(this).find("textarea[name=comments]").lengthValid(0, 350); 
    var boolAbsenceType = $(this).find("select[name=type]").selectValid(1); 



    }); 
} //end of init: function() 
}; //end of addAbsence = {