【问题标题】:grab the value of an element in a different template获取不同模板中元素的值
【发布时间】:2016-02-17 08:00:08
【问题描述】:

下面的代码试图从 template.login 上的输入元素中获取用户名和密码,并在单击 template.footer 中的按钮时创建用户。
作为 javascript 和 Meteor 的新手,我下面的代码可能会破坏两者。

我想从页脚单击事件中进行方法调用,此调用获取用户名和密码并触发 Meteor 帐户创建并将返回的 userId 存储在本地会话中以供以后使用。废话..废话..
但是如何才能从另一个模板访问一个模板中的输入值呢?
所以我需要你的帮助。谢谢

Template.footer.events({
  'click .myClass': function (event, template) {
    Meteor.call('loginUser', username,password);
  }
});

Meteor.methods({
  //store the useId returned from createUser in a local session userId
  loginUser: function (username, password) {
    Accounts.createUser(username, password, function () {
      Session.set(userId, this.value);
    });
  }
});
<template name="footer">
  <footer>
    <nav class="navbar navbar-default navbar-fixed-bottom">
      <div class="container">
        <div class="row">
          {{#each footerButtons}}
            <h2>
              <button class="col-xs-{{footerButtonsScaling}} myClass" type="button">{{text.toUpperCase}}</button>
            </h2>
          {{/each}}
        </div>
      </div>
    </nav>
  </footer>
</template>

<template name="login">
  <form action="">
    <input type="text" name="username" placeholder="type your ID" value="{{username}}">
    <input type="password" name="password" placeholder="type your PIN" value="{{password}}">
  </form>
</template>

【问题讨论】:

    标签: meteor


    【解决方案1】:

    您可以只使用 jQuery 访问 DOM 中任何位置的字段值:

    var username = $('[name="username"]').val();
    var password = $('[name="password"]').val();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多