javascript <select> Meteor.js 上的更改事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17080666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:01:48  来源:igfitidea点击:

change event on <select> Meteor.js

javascriptjqueryeventsdrop-down-menumeteor

提问by koramaiku

I know that Meteor supports events and I have seen it in action on checkboxes, but I just wanted someone to be able clarify if we can hook change events in meteor on select dropdowns as like the following

我知道 Meteor 支持事件并且我已经看到它在复选框上的作用,但我只是希望有人能够澄清我们是否可以在选择下拉列表中挂钩流星中的更改事件,如下所示

 Template.templateName.events({
     'change select': function(e,t){
        // do whatever.......
     }
 });

I am trying to do this using Meteor and it doesn't seem to be firing when I change the value in the select box. However when I use jQuery to change things then it works fine.

我正在尝试使用 Meteor 执行此操作,但当我更改选择框中的值时,它似乎没有触发。但是,当我使用 jQuery 进行更改时,它可以正常工作。

回答by Kristoffer K

Your code should work, it works fine for me.

你的代码应该可以工作,它对我来说很好。

Though I think events only take one eventMap-argument, not two. What would the "t" argument be?

虽然我认为事件只需要一个 eventMap 参数,而不是两个。“t”参数是什么?

回答by Max Hodges

you can use an event like this to handle multiple checkboxes, then check some property (often id) to see which one was clicked. Or here since thisreturns my client row, I can just get the value I want from the context.

您可以使用这样的事件来处理多个复选框,然后检查某些属性(通常是 id)以查看单击了哪个属性。或者这里因为this返回我的客户端行,我可以从上下文中获取我想要的值。

Template fragment

模板片段

 {{#each phonemeResults}}
    <tr>
      <td>
        <div class="checkbox">
          <label>
            <input type="checkbox" class="star">
          </label>
        </div>
      </td>
      <td>
          {{word}}
      </td>
      <td>
        <small>{{phoneme}}</small>
      </td>
    </tr>
  {{/each}}

handler

处理程序

Template.phonemeList.events({
  'change [type=checkbox]': function(e,t){
    console.log("add " this.word + " to user's public starred list");
  },
});

回答by jnhdny

I have a similar issue. The gotcha was that only the <option>elements were included in my template, and the <select>element was in outside it, in <body>. Make sure that the Template the event map is configured actually contains the <select>element.

我有一个类似的问题。问题是只有<option>元素包含在我的模板中,<select>元素在它的外部,在<body>. 确保配置事件映射的模板实际上包含该<select>元素。

回答by Shaun Groenewald

If you want it to fire on a specific select box change:

如果您希望它在特定的选择框更改时触发:

Template.chatRooms.events({
'change #chatroomList' : function(event){
        console.log("Changed")
      }
})

If you want it to fire on any selectbox change in the template:

如果您希望它在模板中的任何选择框更改时触发:

Template.chatRooms.events({
    'change select' : function(event){
            console.log("Changed")
          }
    })