如何使用mithril.js将onchange事件添加到select标记并使select标记也具有选项同级

How to add an onchange event to select tag and have the select tag also have option siblings using mithril.js

本文关键字:select 选项 何使用 事件 onchange 添加 mithril js      更新时间:2023-09-26

我不知道如何将事件和同级元素添加到元素中。

我现在拥有的例子:

m('select', [
  ctrl.countries().map(function(d, i){ 
    return m('option', { onclick : ctrl.country_add.bind(group, group.countries),  value : d.iso2, innerHTML : d.Name })
    console.log(d,i);
  })
])

以及一个我认为可行但不可行的例子。

m('select', { onchange : function(){alert('this')}},[-
  ctrl.countries().map(function(d, i){ 
    return m('option', { value : d.iso2, innerHTML : d.Name })
    console.log(d,i);
  })
])

我希望清楚我的意图。请注意,在示例2中删除了onclick事件,并在其父元素"select"中添加了一个onchange事件。

第二个片段工作正常吗?

m.render(document.body, [
m('select', { onchange : function(){ alert('this') }},[
  [1, 2, 3].map(function(d, i){ 
    return m('option', { value : d, innerHTML : d })
  })
])
]
);

http://jsfiddle.net/3eshvafc/