修复data-services属性JSON解析错误,增加对逗号分隔字符串的支持

This commit is contained in:
2025-12-06 01:39:07 +08:00
parent 44e4bc64d8
commit d387487bc5
+15 -1
View File
@@ -824,7 +824,21 @@ try {
const duration = parseInt(selectedOption.getAttribute('data-duration'));
const price = parseFloat(selectedOption.getAttribute('data-price'));
const servicesJSON = selectedOption.getAttribute('data-services');
const services = servicesJSON ? JSON.parse(servicesJSON) : [];
let services = [];
// 尝试解析servicesJSON,如果解析失败则将其视为逗号分隔字符串
if (servicesJSON) {
try {
// 尝试作为JSON解析
services = JSON.parse(servicesJSON);
console.log('✅ Parsed services as JSON:', services);
} catch (e) {
// 如果JSON解析失败,尝试作为逗号分隔字符串处理
console.log('⚠️ JSON parse failed, treating as comma-separated string:', servicesJSON);
services = servicesJSON.split(',').map(service => service.trim()).filter(service => service.length > 0);
console.log('✅ Converted to array:', services);
}
}
console.log('Package data:', {duration, price, services: services.length});