Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的函数不是用Meteor定义的?_Javascript_Html_Meteor - Fatal编程技术网

Javascript 为什么我的函数不是用Meteor定义的?

Javascript 为什么我的函数不是用Meteor定义的?,javascript,html,meteor,Javascript,Html,Meteor,我的site.html文件中有以下行 <input type="text" name="income" id="income" onkeydown="myFunction(this.value)"> 因此,本质上我需要从输入字段中获取值,要么使用blur,要么按类型(onkeydown)并将其添加到局部变量“total”中,该变量将显示在页面的其他地方。由于某些原因,当我在控制台中键入“myFunction is not defined”时,我的函数不起作用。我需要在哪里准确地定

我的site.html文件中有以下行

 <input type="text" name="income" id="income" onkeydown="myFunction(this.value)">

因此,本质上我需要从输入字段中获取值,要么使用blur,要么按类型(onkeydown)并将其添加到局部变量“total”中,该变量将显示在页面的其他地方。由于某些原因,当我在控制台中键入“myFunction is not defined”时,我的函数不起作用。我需要在哪里准确地定义它(在我的.html文件中不应该使用JavaScript)。

这里有一个适用于blur事件的版本。 标记:


函数
myFunction
必须在任何其他函数(全局范围)之外声明,否则像
onkeydown=“myFunction(this.value)”
这样的内联事件处理程序无法看到该函数。更好的做法是使用
addEventListener
绑定事件处理程序(您说过“我的.html文件中不应使用JavaScript”,但仍然使用内联事件侦听器)。以下是您的答案可能与感谢重复,我最后使用了“change#income”,因为输入在我的情况下不起作用(数字相加的问题)。但是,是的,这就是我要找的。我想多研究一些事件引用是个好主意,因为它们非常有用。再次感谢。
    if (Meteor.isClient) {
      function myFunction(val) {
        var income = document.getElementById("income").value;
      }
      var total = total + income;
    }

    if (Meteor.isServer) {
      Meteor.startup(function () {
        // code to run on server at startup
      });
    }
<body>
  {{> hello}}
</body>

<template name="hello">
  <input type="text" name="income" id="income">
  <h2>{{Total}}</h2>
</template>
if (Meteor.isClient) {
  // counter starts at 0
  Session.set('total', 0);

  Template.hello.helpers({
    Total: function() {
      return Session.get('total');
    },
  });

  Template.hello.events({
    'blur #income': function (event, template) {
      // increment the counter when button is clicked
      Session.set('total', Session.get('total') + Number(event.target.value));
      event.target.value = '';
    },
  });
}