gpt4 book ai didi

javascript - 如何停止 Google 表单中的 Onsubmit

转载 作者:行者123 更新时间:2023-11-30 13:01:35 26 4
gpt4 key购买 nike

在谷歌表单中,我编写了一个验证检查来检查一个字段,如果它超过了一个计数,那么我需要停止表单提交并提醒用户错误。我可以将错误文本添加到 div,但无法停止表单提交。

如何停止表单提交。我已经尝试过 e.preventDefault() 但它没有用。任何人都可以帮忙。我附上了在谷歌表单中提交表单时调用的代码。

function onFormSubmit(e) {
Logger.log("test log", "testing");
var timestamp = e.values[0];
var programname = e.values[1];
var center = e.values[2];
var fullname = e.values[3];
var board = e.values[4];
var programtype = e.values[5];
var programobjective = e.values[6];
var calltoaction = e.values[7];
var begindate = e.values[8];
var enddate = e.values[9];
var marketing = e.values[10];
var budget = e.values[11];
var expectedparticipation = e.values[12];
var registrationfee = e.values[13];
var announcement = e.values[14];
var insightannouncement = e.values[15];
var additionallanguage = e.values[16];
var checklist = e.values[17];
var speakerprofile = e.values[18];
var noticeboarditem = e.values[19];
var toAddress = e.values[20];
var programcategory = e.values[21];
var liability = e.values[22];
var datesofannouncement = e.values[23];
var explaination = e.values[24];
var additionalquestion = e.values[25];
var location = e.values[26];
var externalpartners = e.values[27];
var intergenerational = e.values[28];
var announcementlanguage = e.values[29];

// var app = DocumentApp.getUi();

var app = UiApp.getActiveApplication();

Logger.log("test log in checkAnnouncement: " + announcement + " insight " + insightannouncement, "testing");
// var announcement = e.values[14];
//var insightannouncement = e.values[15];
//var toAddress = e.values[20];
//var htmlBody = "Thank you for your <b>Program Package</b> report submitted on <i>";
//var subject = "test subject";
Logger.log(announcement, "testing");
Logger.log(insightannouncement, "testing");
// Logger.log(toAddress, "testing");

announcement = announcement.replace(/(^\s*)|(\s*$)/gi,"");
announcement = announcement.replace(/[ ]{2,}/gi," ");
announcement = announcement.replace(/\n /,"\n");

insightannouncement = insightannouncement.replace(/(^\s*)|(\s*$)/gi,"");
insightannouncement = insightannouncement.replace(/[ ]{2,}/gi," ");
insightannouncement = insightannouncement.replace(/\n /,"\n");

if(announcement.split(' ').length > 3)
{
app.getElementById('entry_15').setText("*Announcement should be less than 30 words").setStyleAttribute("color", "#F00");
e.preventDefault();
}
else if(insightannouncement.split(' ').length > 5)
{
app.getElementById('entry_32').setText("*insightAnnouncement should be less than 50 words").setStyleAttribute("color", "#F00");
e.preventDefault();
}

}

最佳答案

Google 表单的数据验证仅限于提交后,例如调用 onFormSubmission 触发器时。在那个时间点,无法与表单 UI 进行交互。

触发函数收到的事件是 FormResponse对象,它不包含 preventDefault() 方法。

引用资料

关于javascript - 如何停止 Google 表单中的 Onsubmit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17282563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com