55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
Component({
|
||
data: {
|
||
// 用对象存储多个卡片的选中状态,键为卡片ID,值为选中的选项ID
|
||
selectedOptions: {} as Record<string, string>
|
||
},
|
||
methods: {
|
||
/**
|
||
* 选择选项
|
||
* @param e 事件对象,包含卡片ID和选项ID
|
||
*/
|
||
selectOption(e: WechatMiniprogram.TouchEvent) {
|
||
const { card, id } = e.currentTarget.dataset as { card: string; id: string };
|
||
|
||
// 更新选中状态(使用对象展开语法保持响应式)
|
||
this.setData({
|
||
selectedOptions: {
|
||
...this.data.selectedOptions,
|
||
[card]: id
|
||
}
|
||
});
|
||
},
|
||
|
||
/**
|
||
* 提交投票
|
||
* @param e 事件对象,包含卡片ID
|
||
*/
|
||
submitVote(e: WechatMiniprogram.TouchEvent) {
|
||
const { card } = e.currentTarget.dataset as { card: string };
|
||
const selected = this.data.selectedOptions[card];
|
||
|
||
if (!selected) {
|
||
wx.showToast({
|
||
title: '请先选择一个选项',
|
||
icon: 'none',
|
||
duration: 2000
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 可以通过triggerEvent向父组件传递投票结果
|
||
this.triggerEvent('voteSuccess', {
|
||
cardId: card,
|
||
selectedOption: selected,
|
||
timestamp: Date.now()
|
||
});
|
||
|
||
wx.showToast({
|
||
title: '投票成功',
|
||
icon: 'success',
|
||
duration: 2000
|
||
});
|
||
}
|
||
}
|
||
});
|
||
|