【问题标题】:How do you change URL path with a button when using Meteor JS使用 Meteor JS 时如何使用按钮更改 URL 路径
【发布时间】:2016-06-15 10:50:50
【问题描述】:

我知道当您想通过单击纯 html 中的某个元素来加载另一个 html 文件时,您会使用如下内容:

<a href="index2.html">Example Link</a>

但是我应该如何在 Meteor 应用程序中执行此操作。我的情况是我想单击一个提交按钮,并且让该按钮不仅提交我拥有的某个表单,而且将路径更改为另一个页面。现在在我的流星应用程序中,我正在使用 kadira 流路由器并设置了一些目录,所以我想我只需创建一个 onclick 函数来呈现该流路由器。这是我为此尝试的代码:

路线代码:

FlowRouter.route('/joinLobby', {
  name: 'joinLobby',
  action() {
    BlazeLayout.render('joinLobby');
  }
});

点击功能:

<script>
  function renderPage(){
    BlazeLayout.render('joinLobby');
  }
</script>

按钮:

<button type="submit" class="btn btn-primary"    onclick="renderPage()">Insert</button>

我不确定这是否是最佳做法,这只是我拼凑到另一个页面的结果,所以我想问最好的方法是什么。当我使用它时,这也给我带来了一些问题。主要问题是当我单击按钮时,它会将我弹回默认路线并说 localhost:3000 无法加载图像,因此我什至无法到达该路线。

【问题讨论】:

    标签: javascript html debugging meteor hyperlink


    【解决方案1】:

    您没有以流星方式处理事件。应采用以下方式:

    <button type="submit" class="btn btn-primary btn-insert">Insert</button>
    

    模板事件:

    Template.template_name.events({
      "click .btn-insert": function() {
        //Your logic to submit form goes here...
        //on validating the form data and posting it successfully, navigate to the new route...
        FlowRouter.go('/joinLobby');
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 2022-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      相关资源
      最近更新 更多