使用Apps Script开发预约助手时HTML select选项提交后不更新
问题根因
google.script.run是异步调用,你之前直接在点击事件后加loadAppt(),会在后端预约数据还没更新完成时就提前拉取旧的时段列表- 原有
loadAppt函数没有清空旧选项,会出现选项重复的问题
解决方法
你只需要修改HTML侧的两个JS函数即可实现无需刷新侧边栏自动更新下拉选框:
1. 优化loadAppt函数,先清空原有下拉选项,避免重复
function loadAppt() { google.script.run.withSuccessHandler(function(ar) { var apptSelect = document.getElementById("dateTime"); // 先清空现有所有选项 apptSelect.innerHTML = ''; var option = document.createElement("option"); option.value = ""; option.text = ""; apptSelect.appendChild(option); ar.forEach(function(item, index){ var option = document.createElement("option"); option.value = [item[0], item[1]]; option.text = [item[0], item[1]]; apptSelect.appendChild(option); }); }).getList(); };
2. 修改addCustomerAppt函数,等后端预约提交完成后再刷新下拉选项
function addCustomerAppt() { var dateTime = document.getElementById('dateTime').value; var firstName = document.getElementById('firstName').value; var lastName = document.getElementById('lastName').value; var email = document.getElementById('email').value; var phone = document.getElementById('phone').value; // 监听后端执行完成的回调,完成后刷新下拉框 google.script.run.withSuccessHandler(function() { loadAppt(); // 可选:提交完成后清空表单输入,方便下次录入 document.getElementById('firstName').value = ''; document.getElementById('lastName').value = ''; document.getElementById('email').value = ''; document.getElementById('phone').value = ''; }).newAppointment(dateTime, firstName, lastName, email, phone); }
修改完成后点击添加预约按钮,等后端数据更新完成后会自动重新拉取最新的可用时段更新下拉选框,不需要手动关闭或刷新侧边栏。
内容的提问来源于stack exchange,提问作者austin fogle
相关产品推荐
相关产品推荐

