【问题标题】:Create text field that counts the number of words live in Meteor创建计算 Meteor 中的单词数的文本字段
【发布时间】:2015-09-16 22:01:32
【问题描述】:

我想创建一个文本字段,在有人输入时计算其中的字数,并在视图中显示这个数字,当点击提交时,它会保存字数,但不保存文本本身。实现这一目标的任何提示?

谢谢!

【问题讨论】:

  • var numberOfWords = text.split(' ').length;
  • 所有发布的都是程序描述。但是,我们需要您ask a question。我们无法确定您想从我们这里得到什么。请edit您的帖子包含一个我们可以回答的有效问题。提醒:请确保您知道what is on-topic here,要求我们为您编写程序,建议是题外话。 “我怎么做?”不会飞。
  • 这不是一个问题吗?对不起。

标签: meteor


【解决方案1】:

可以在不使用全局变量的情况下用更少的代码来完成,但这是他们在 Meteor 官方教程中的做法,所以你应该已经很熟悉了。

我提供了working example on MeteorPad

HTML:

<body>
  <input type="text">
  <p>{{counter}}</p>
</body>

JS:

Template.body.helpers({
  counter: function () {
    return Session.get('count');
  }
});

Template.body.events({
  'keydown input': function () {
    Session.set('count', $('input').val().split(' ').length + ' words');
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多