0
我想在选择选项中使用@click。在选择选项中使用@click - Vue.js 2
到目前为止,我有:
<button @click="sortBy('name')">sort by name</button>
<button @click="sortBy('price')">sort by price</button>
和它的作品,但是当我将其插入选项标签,它停止工作。
<select name="sortBy" id="sortBy">
<option value="sort">sort By</option>
<option @click="sortBy('name')">name</option>
<option @click="sortBy('price')">price</option>
</select>
我的功能:
sortBy(sortKey) {
this.items.sort((a, b) =>
(typeof a[sortKey] === 'string' || typeof b[sortKey] === 'string') ?
a[sortKey].localeCompare(b[sortKey]) : a[sortKey] - b[sortKey]);
}
非常感谢! 还有一个问题:是否可以在每个选择选项上使用不同的功能? – Natalia
您不能直接使用不同的函数,因为浏览器只支持将DOM事件绑定到'select',no'option'。 – JiangangXiong