从外部javascript加载函数“未定义”

时间:2013-11-21 18:13:12

标签: javascript jquery

我有两个文件:index.php和local.js。

这是index.php:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="../../docs-assets/ico/favicon.png">

    <!-- FontAwesome: font-set designed for Bootstrap -->
    <link href="css/font-awesome.css" rel="stylesheet">

    <title>Befair-Timetracker</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/time.css" rel="stylesheet">

    <!-- Template HTML to build up a new row on the table -->


    <!-- Just for debugging purposes. Don't actually copy this line! -->
    <!--[if lt IE 9]><script src="../../docs-assets/js/ie8-responsive-file-warning.js"></script><![endif]-->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->


  </head>

  <body>
    <?php
        //include("dbconn.php");
    ?>

    <div class="navbar navbar-inverse navbar-fixed-top navbar-time" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle navbarbtn" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Timetracker</a>
        </div>
        <div class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Tracking</a></li>
          </ul>
          <form class="navbar-form navbar-right">
            <div class="form-group">
              <input type="text" placeholder="Email" class="form-control input-sm">
            </div>
            <div class="form-group">
              <input type="password" placeholder="Password" class="form-control input-sm">
            </div>
            <button type="submit" class="btn btn-success">Sign in</button>
          </form>
        </div><!--/.navbar-collapse -->
      </div>
    </div>

    <!-- Main jumbotron for a primary marketing message or call to action -->
    <div class="jumbotron jumbotime">
      <div class="container">
        <h1 class="time">Timetracker management</h1>
        <p class="timepar">Nella tabella sottostante verranno inserite la varie attività, classificabili in base al proprio CH e agli utenti che le svolgono.</p>
        <!--<p><a class="btn btn-primary btn-lg" role="button">Learn more &raquo;</a></p>-->
      </div>
    </div>

    <div class="container">
        <h3 class="time">Risultati attesi</h3>
            <hr>
        <div class="selection"> <!-- inserire well nella classe per ottenere riquadro blu -->
            <span class="titolosel">Centro di costo</span>
            <select class="form-control selecttime">
            </select>                
        </div>
            <hr>

    <!-- Table containing the info about CH, users and RA -->    
    <table id ="timetable" class="table table-striped">
        <thead>
          <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Users</th>
            <th>Activities</th>
            <th>Timer</th>
          </tr>
        </thead>
        <tbody class="ra hide">

        </tbody>
      </table>    


      <footer>
        &copy; BeFair 2013 - Website developed by Riccardo Pancotti
        <div class="pull-right">          
          <!--<img src="images/beFair.jpg" alt="beFair-logo" class="img-rounded imglogo">-->  
        </div>
      </footer>
    </div> <!-- /container -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->

    <script src="js/jquery-1.10.2.min.js"></script>
    <script src="js/bootstrap.js"></script>
    <script type="text/javascript" src="js/jquery.tmpl.min.js"></script>
    <script type="text/javascript" src="js/jquery.tablesorter.min.js"></script>
    <script type="text/javascript" src="js/local.js"></script>


    <script type="text/javascript" charset="utf-8">
    //**ANIMATED DIV/TABLE/TIMER**
    function showact(ID){
        $(".tr-act"+ID).animate({"height": "toggle"}, { duration: 300 });
        //$(".hide").animate({"display":"toggle"}, { duration: 300 });
        }   
    </script>

    <script type="text/javascript" charset="utf-8">
            //**LOADING JSON**  
                $(document).ready(function()
                    {
                        starttimer(2);
                        $.getJSON('js/test2.json', function(json) {                           
                                $.each(json.ch,function(key,val)
                                {
                                    var chname=("<option value="+val.name+">"+val.name+"</option>");
                                    $(".selecttime").append(chname);
                                });                             
                        });
                    });



   </script>

   <script type="text/javascript" charset="utf-8">
       //$(document).ready(startimer(4));          
       //**CENTRO DI COSTO SELECTION**    
       $('.selecttime').change(function() 
        {
            var pippo = "";
            console.clear();
            //setValue();



            var pippo = $('.selecttime').val();          
            var chtable=("<tbody class=\"ra\"><tr>");
            $.ajaxSetup( { "async": false } ); //Chiamata asincrona al server per il JSON
            $.getJSON('js/test3.json', pippo,function(json) {                           

                        for (var i = 0, len = json.ch.length; i < len; i++) {

                            var namera = json.ch[i].name;
                            console.log(namera);

                                if (pippo === namera)
                                {
                                    console.log("RA " +json.ch[i].ra_list.length);
                                    for (var k = 0, lungh = json.ch[i].ra_list.length; k < lungh; k++)
                                    { 
                                        //CICLO PER AGGIUNGERE NUOVE RA
                                        var print = json.ch[i].ra_list[k];

                                        chtable +=("<td>"+print.id+"</td>");
                                        chtable +=("<td>"+print.name+"</td>");
                                        chtable +=("<td>"+print.users+"</td>");
                                        //***VECCHIA RIGA, MOSTRA TIMER***
                                        //chtable +="<td><a href=\"#\" onClick=\"showact("+k+"); return false;\"><i class=\"fa fa-plus-square\"></i></a></td>";
                                        //********************************
                                        chtable +="<td class=\"timertd\"><a href=\"#\" onClick=\"showact("+k+"); return false;\"><i class=\"fa fa-plus-square\">+</i></a></td>";
                                        chtable +="<td></td>";
                                        console.log("Attività " + print.activities.length);
                                        tract = "";
                                        list_act = "";
                                        count = 0;

                                        for (var j = 0, lungh_in = json.ch[i].ra_list[k].activities.length; j < lungh_in; j++)
                                        {                                       
                                          //CICLO PER AGGIUNGERE NUOVE ATTIVITA'
                                          list_act += ("<tr style=\"display:none\" class=\"tr-act"+k+"\">");
                                          list_act += "<td></td><td></td><td></td>";                                  
                                          list_act += "<td>";
                                          list_act += print.activities[j].name;                                         
                                          list_act += "</td>";
                                          list_act += "<td>" + "setValue()" + "</td>";
                                          list_act += ("</tr>");                                           
                                          count++;                                
                                        }

                                            chtable += list_act;
                                            chtable += "</tr>";
                                            $(".ra").replaceWith(chtable);
                                    }                                                             
                                }
                        }               
                        });
        });
     </script>

  </body>
</html>

这是local.js:

    var starttimer = function(idt)
    {
        alert(idt);
    };

我试图在这里调用此函数(它是index.html的一部分):

 <script type="text/javascript" charset="utf-8">
            //**LOADING JSON**  
                $(document).ready(function()
                    {
                        starttimer(2);
                        $.getJSON('js/test2.json', function(json) {                           
                                $.each(json.ch,function(key,val)
                                {
                                    var chname=("<option value="+val.name+">"+val.name+"</option>");
                                    $(".selecttime").append(chname);
                                });                             
                        });
                    });



   </script>

但它一直告诉我“功能未定义”。 有什么问题? 可能是关于范围的事情,但我不知道是什么。

1 个答案:

答案 0 :(得分:0)

解决,我指的是错误的文件。