为什么我的功能没有用Meteor定义?

时间:2015-10-04 08:52:45

标签: javascript html meteor

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

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

我有一个单独的site.js文件,如下所示:

    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
      });
    }

所以基本上我需要通过blur或as-typed(onkeydown)从输入字段中获取值,并将其添加到页面上其他位置显示的局部变量“total”。由于某些原因,当我键入时,我的功能无效,我在控制台中得到“myFunction未定义”。我需要在哪里准确定义它(我的.html文件中不应使用JavaScript)。

1 个答案:

答案 0 :(得分:1)

这是一个适用于模糊事件的版本。 标记:

<body>
  {{> hello}}
</body>

<template name="hello">
  <input type="text" name="income" id="income">
  <h2>{{Total}}</h2>
</template>

和javascript:

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 = '';
    },
  });
}
相关问题