按钮单击未连接到Javascript

时间:2017-11-22 16:06:06

标签: javascript html api button

我正在尝试将我的html中的按钮连接到我的javascript文件。我已经使用这种方法几个星期,突然间它停止工作,现在我收到一个错误,"未在HTMLInputElement.onclick"中定义了will_be_set_after_endpoints_apis_loaded。当我把它改成像#34; greetByName();"这样的函数调用时它说没有定义的方法让我觉得javascript文件没有被识别。我将分享我的代码片段:

HTML:

 <!DOCTYPE html>
     <html>
     <head>
     <title>FLINDER</title>
     <meta charset="UTF-8">
    <script src="hello.js"></script>

<!--  Load the Google APIs Javascript client library -->
<!--  Then call the init function, which is defined in hello.js -->
<script src="https://apis.google.com/js/client.js?onload=init">
 </script>
 </head>

<meta name="viewport" content="width=device-width, initial-scale=1">


  <P><input id="input_greet_name" type="button" value="Please wait..." 
  onclick="will_be_set_after_endpoints_apis_loaded"></P>

JS文件:

 package com.google.training.helloworld;

 //<script type="text/javascript" 
src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
 </script>

  function init() {

// You need to pass the root path when you load your API
// otherwise calls to execute the API run into a problem

// rootpath will evaulate to either of these, depending on where the 
 app is running:
// //localhost:8080/_ah/api
// //your-app-id/_ah/api

var rootpath = "//" + window.location.host + "/_ah/api";

// Load the helloworldendpoints API
// If loading completes successfully, call loadCallback function
gapi.client.load('helloworldendpoints', 'v1', loadCallback, rootpath);
}

/*
  * When helloworldendpoints API has loaded, this callback is called.
 * 
 * We need to wait until the helloworldendpoints API has loaded to
 * enable the actions for the buttons in index.html,
 * because the buttons call functions in the helloworldendpoints API */


        function loadCallback () {  
      // Enable the button actions
       enableButtons ();
        }

function enableButtons () {
// Set the onclick action for the first button
//btn = document.getElementById("input_greet_generically");
//btn.onclick= function(){greetGenerically();};

// Update the button label now that the button is active
//btn.value="Submit";

// Set the onclick action for the second button
btn = document.getElementById("input_greet_name");
btn.onclick= function(){greetByName();};

// Update the button label now that the button is active
btn.value="Submit";
}
function greetByName () {

         var loc;
         var sal;
         var schoolTypes;
         var request;
         loc = document.getElementById("loc").value;
         sal = document.getElementById("sal").value;
         var temp = loc;
         schoolTypes = document.getElementById("schoolTypes").value;
         var temps = schoolTypes;
         if(loc == "" || sal == "" || schoolTypes == "")
         {
         alert("Please enter all valid options");
        request = gapi.client.helloworldendpoints.sayHello();
         }
}

0 个答案:

没有答案