簡體   English   中英

我無法在工作中使用此JFidddle代碼

[英]I can't get this JFidddle Code to work in my project

我是JavaScript新手,在編寫代碼時仍在學習。 我想在項目中實現此特定的JSFiddle代碼( http://jsfiddle.net/MCzJ6/163/ ),但是它不起作用。 任何幫助將是非常感謝。

<!doctype html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
</head>
<script type="text/javascript">
  ;(function($, window, document, undefined){
    $("#days").on("change", function(){
       var date = new Date($("#start_date").val()),
           days = parseInt($("#days").val(), 10);

        if(!isNaN(date.getTime())){
            date.setDate(date.getDate() + days);

            $("#end_date").val(date.toInputFormat());
        } else {
            alert("Invalid Date");  
        }
    });


    //From: http://stackoverflow.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object
    Date.prototype.toInputFormat = function() {
       var yyyy = this.getFullYear().toString();
       var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
       var dd  = this.getDate().toString();
       return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]); // padding
    };
})(jQuery, this, document);</script>
<body>
    <input type="date" id="start_date" placeholder="Start Date"/>
<input type="number" id="days" min=0 placeholder="Days"/>
<input type="date" id="end_date" placeholder="End Date" readonly/></body>
</html>
    <!doctype html>
    <html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>


<script type="text/javascript">
      (function($, window, document, undefined){
        $("#days").on("change", function(){
           var date = new Date($("#start_date").val()),
               days = parseInt($("#days").val(), 10);

            if(!isNaN(date.getTime())){
                date.setDate(date.getDate() + days);

                $("#end_date").val(date.toInputFormat());
            } else {
                alert("Invalid Date");  
            }
        });


        //From: http://stackoverflow.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object
        Date.prototype.toInputFormat = function() {
           var yyyy = this.getFullYear().toString();
           var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
           var dd  = this.getDate().toString();
           return yyyy + "-" + (mm[1]?mm:"0"+mm[0]) + "-" + (dd[1]?dd:"0"+dd[0]); // padding
        };
    })(jQuery, this, document);
</script>




    </head>
    <body>
        <input type="date" id="start_date" placeholder="Start Date"/>
    <input type="number" id="days" min=0 placeholder="Days"/>
    <input type="date" id="end_date" placeholder="End Date" readonly/></body>
    </html>

腳本應該在正文或頭部。 在腳本的開頭和函數之前也有一個分號,導致該異常。 看到這個,它應該可以正常工作。

您有一些語法錯誤。 請研究此工作代碼以了解您的去向。 你近了 嘗試第一個應用程序!

問題的一個很好的標題是,例如,嘗試使Date計算器正常工作時出現語法錯誤或類似的錯誤。

還要注意,我是如何在Stack Overflow中放置“小提琴”(稱為代碼段)的。 您也可以在問題中這樣做。

 $("#days").on("change", function() { var date = new Date($("#start_date").val()), days = parseInt($("#days").val(), 10); if (!isNaN(date.getTime())) { date.setDate(date.getDate() + days); $("#end_date").val(date.toInputFormat()); } else { alert("Invalid Date"); } }); //From: http://stackoverflow.com/questions/3066586/get-string-in-yyyymmdd-format-from-js-date-object Date.prototype.toInputFormat = function() { var yyyy = this.getFullYear().toString(); var mm = (this.getMonth() + 1).toString(); // getMonth() is zero-based var dd = this.getDate().toString(); return yyyy + "-" + (mm[1] ? mm : "0" + mm[0]) + "-" + (dd[1] ? dd : "0" + dd[0]); // padding }; 
 <!doctype html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <input type="date" id="start_date" placeholder="Start Date" /> <input type="number" id="days" min=0 placeholder="Days" /> <input type="date" id="end_date" placeholder="End Date" readonly/> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM