【问题标题】:Get option value from select element after change更改后从选择元素获取选项值
【发布时间】:2016-06-15 19:28:41
【问题描述】:

此 Meteor 客户端代码假设在更改后从 select html 元素中选择值,但目前每次单击选择元素时它都会获取整个元素。
我怎样才能得到元素的值,只有在它被改变之后?谢谢

这里的选项是什么样的

<select name="foodItem>
   <option value="0">        
   <option value="1">        
</select>



Template.food.events({
  "click select[name='foodItem']": function(event, opt) {
    console.log(opt.find(':selected'));
  }
});

【问题讨论】:

  • 尝试change 事件
  • @PranavCBalan 谢谢,我如何只获得value 属性的值?
  • opt.find(':selected').value
  • 另外,一旦切换到使用clickevent.target.value 将包含该值,因为targetselect 元素,对于单选,其value 属性将包含值。根本不需要opt.find
  • @PranavCBalan:我在the Meteor docs I quickly found 中找不到任何关于存在第二个论点的信息,所以我只选择了event.target。但由于我不了解 Meteor,可能只是发现了不完整或过时的信息。

标签: javascript meteor


【解决方案1】:

改用更改事件

Template.food.events({
  "change select[name='foodItem']": function(event, opt) {
    console.log(opt.find(':selected'));
    // to get selected value use 
    // event.target.value
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 2011-12-21
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多