【发布时间】:2014-08-12 13:06:54
【问题描述】:
我在使用 Google Apps 脚本 HTML 服务时尝试使用 HTML5 表单验证。如another user asked,根据documentation example,您必须使用按钮输入而不是提交输入。使用提交按钮似乎可以进行验证,但无论如何都会调用服务器函数。给该用户的答案对我不起作用。另外,我想在提交表单时调用两个函数,这会使它变得更复杂。
这就是我想要做的:用户填写表格,我生成一个 Google Doc 并给他 url。当他单击提交按钮时,我向他展示了一个带有漂亮微调器的 jQuery UI 对话框,上面写着“正在创建您的文档”。然后,当文档生成时,我给他链接。当 Google Doc 内容完成时,我使用成功处理程序来显示结果,但同时我需要一个函数来显示微调器。我不知道是否有比在 onclick 事件中添加另一个函数更好的方法来做到这一点,也许它可能会以某种方式破坏进程。如果表单无效(使用 HTML5 验证),有没有办法不调用任何这些函数?
这是我的代码的简化版本:
代码.gs
function generateDocument(formObject) {
var doc = DocumentApp.create("Document name");
...
return doc.getUrl();
}
页面.html
<main>
<form id="myForm">
...
<input type="button" value="Generate document"
onclick="showProgress();
google.script.run
.withSuccessHandler(openDocument)
.generateDocument(this.parentNode);"/>
</form>
<div id="dialog-confirm" title="Your document">
<div id="dialog-confirm-text"></div>
</div>
Javascript.html
$( "#dialog-confirm" ).dialog({ autoOpen: false, resizable: false, modal: true });
function showProgress() {
$( "#dialog-confirm" ).dialog({ buttons: [ { text: "Cancel", click: function() { $( this ).dialog( "close" ); } } ] });
$( "#dialog-confirm" ).dialog( "open" );
$( "#dialog-confirm-text" ).html( "<br />Wait a second, your document is being generated...<br /><br /><img src='http://i.stack.imgur.com/FhHRx.gif' alt='Spinner'></img>" );
return false;
}
function openDocument(url) {
$( "#dialog-confirm" ).dialog({ autoOpen: false, resizable: false, width: 400, buttons: [ { text: "Ok", click: function() { $( this ).dialog( "close" ); } } ] });
$( "#dialog-confirm-text" ).html( '<br /><a href="' + url + '" target="_blank">Click here to open and print your document!</a>' );
return false;
}
所有三个 HTML 文档都通过包含函数 as recommended in the documentation 连接在一起(并使用其各自的标签)。
我是 jQuery 的新手,欢迎任何建议。
最后一个问题:对话框中的取消按钮将关闭它,但不会停止创建文档。是否可以停止此过程?我不这么认为。
非常感谢您。
【问题讨论】:
-
你的代码对我有用...
-
使用此代码,我在表单字段上获得了 HTML5 验证,但它并没有停止调用服务器和 javascript 函数,因此验证对结果没有影响,并且继续执行。
-
它运行是因为脚本函数与 onclick() 绑定。因此,无论何时单击该按钮,它都会运行,无论表单是否经过验证。以下解决方案有效,因为在处理验证逻辑的单独函数中调用了 google 脚本。
if(formValidated){run google script} -
那么,如果您从按钮或表单标签调用该函数(我的意思是,虽然您不直接调用 Google Script 函数),它仍然可以工作?或者按钮不会在错误的 HTML5 验证后停止?这是我对桑迪的回答的问题。
-
绑定对表单对象“onsubmit”的调用将使提交不会在基本的 html5 验证发生时触发,例如您的输入之一中的“必需”标签。但是如果你在提交函数中使用更复杂的验证,那么提交仍然会被触发。这就是为什么 Sandy 在验证 if() 语句中调用 google 脚本的原因。
标签: jquery jquery-ui google-apps-script jquery-ui-dialog html5-validation