【问题标题】:meteor handle form input流星句柄表单输入
【发布时间】:2016-03-01 00:40:19
【问题描述】:

我正在使用流星构建一个显示股票信息的网络应用程序。我目前在客户端有一个输入文本。输入连接到 Markit on Demand 的 Lookup API。这是代码:

Template.LookupTemplate.onRendered(function() {
this.$("#Lookup")
.focus()
.autocomplete({
  source: function(request,response) {
    $.ajax({
      url: "http://dev.markitondemand.com/api/v2/Lookup/jsonp",
      dataType: "jsonp",
      data: {
        input: request.term
      },
      success: function(data) {
        response( $.map(data, function(item) {
          return {
            label: item.Name + " (" +item.Exchange+ ")",
            value: item.Symbol
          }
        }));
      },
      minLength: 0,
              select: function(event,ui ) {
        console.log(ui.item);
      }
    });
  }
});

}); //Closing tag of LoookupTemplate.onRendered

如何捕捉客户的选择?当用户开始输入公司名称时,jquery 自动完成功能会启动并为客户提供可供选择的选项列表。一旦用户选择它并点击“输入”(提交),页面就会重新加载到

http://localhost:3000/Overview?stockname=AAPL

我如何捕获该输入(在本例中为 AAPL),然后将其传递给为该特定股票构建图表的另一个函数?

--Router.js

Router.configure({

// 这是默认布局/顶级模板 布局模板:'布局' });

Router.map(function() {
    this.route('/', {
        path: '/',
        action: function() {
            this.redirect('landingpage')
        document.title = 'Cash'
    }
    });

  // Route for the landing page when user is not logged in
    this.route('landingpage', {
        path: '/landingpage',
        after: function() {
        document.title = 'Cash'
    }
    });

// Route to our main app. Note that I use / path as I treat this as default behavior
    this.route('/Overview', {
        path: '/Overview',
        after: function () {
            document.title = 'Cash';
        }
    });
})

【问题讨论】:

  • 加载到下一页时为什么不使用路由器读取参数?
  • 我该怎么做?我正在使用铁:路由器

标签: javascript jquery api meteor


【解决方案1】:

IronRouter 只有在路由器中定义参数时才能读取参数。对于 API 动态创建参数的情况,最好的方法是使用 javascript 来读取它。请查看here中的解决方案

【讨论】:

  • 我在 Template.LookupTemplate.onRendered 的末尾添加了一个 console.log(Router.current().params['stockname'])。我知道最初它应该给我未定义。但是在选择并被重定向到localhost:3000/Overview?stockname=AAPL 之后,我仍然将console.log 设置为未定义。
  • 那么,用户选择它后,你将它重定向到同一页面吗?
  • 在选择 url 之前是:localhost:3000/Overview。客户端从下拉列表中选择并提交后,页面被重定向到localhost:3000/Overview?stockname=*一些股票名称*。不是我做的。
  • 编辑了原帖
  • 谢谢。会检查的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
相关资源
最近更新 更多