使用jQuery

时间:2015-09-18 17:32:21

标签: javascript jquery html google-apps-script safari

我有以下javascript函数,如果所有必填字段都不完整并且是我使用Google Apps脚本创建的表单的一部分,则会阻止表单提交。请注意,#submitbutton实际上是一个常规按钮,Google Apps Scripts强制使用严格的javascript。该脚本在chrome上工作正常,但是当我在Safari或Firefox上测试它时,它似乎没有运行。我使用的方法仅适用于Chrome吗?我在这里错过了什么吗?

我在Safari中遇到以下错误: Uncaught TypeError: undefined is not a function (evaluating '(1, $)('#submitbutton')')Uncaught Strict mode does not allow function declarations in a lexically nested statement.

Firefox提出了类似的问题:Uncaught in strict mode code, functions may be declared only at top level or immediately within another function 2699307207-maestro_htmlapp_bin_maestro_htmlapp.js:84:360Uncaught TypeError: $ is not a function

<script type="text/javascript">
$('#submitbutton').on('click', function() {
   $(this).val("Submitting...");
   //check for required fields
   var emptyFields = $('[required]').filter(function() {
       $(this).removeClass("warning");
       if ($(this).val().length === 0){
         $(this).addClass("warning")
         return true
       } else {
         return false
       }
   });

   if (emptyFields.length === 0) {
       $(this).prop('disabled', true);
       document.getElementById('incompleteWarning').style.display = 'none';
       document.getElementById('bePatient').style.display = 'inline';
       google.script.run.withSuccessHandler(fileUploaded).uploadFiles(this.parentNode);
    } else{
      $(this).val("Submit Application")
      document.getElementById('incompleteWarning').style.display = 'inline';
      document.getElementById('incompleteWarning').style.color = 'red';
    }
});
</script>

jsfiddle here:https://jsfiddle.net/hofresre/

请注意,jsfiddle不使用google.script,但将其替换为this.parentNode.submit()

非常感谢任何帮助。

每条评论:

maestro_htmlapp_bin_maestro_htmlapp.js第84行:84:360: function ys(a){for(var b=[],c=0;c<a.length;++c){var d=a[c];b[c]=Mn(d)?(new String(d)).toString():d}return b}var As=[vh,"[object Object]"];var Bs=["alert",ts(function(a){return alert(a)},16,[4]),"confirm",ts(function(a){return confirm(a)},16,[4]),"prompt",ts(function(a,b){return prompt(a,b)},16,[4,4])];var Cs=window.console&&window.console.error?function(a){window.console.error(a)}:void 0,Ds=window.console&&window.console.info?function(a){window.console.info(a)}:void 0,Es=window.console&&window.console.log?function(a){window.console.log(a)}:void 0,Fs=window.console&&window.console.warn?function(a){window.console.warn(a)}:void 0,Gs=["console.debug",ts(window.console&&window.console.debug?function(a){window.console.debug(a)}:void 0,16,[4]),"console.error",ts(Cs,16,[4]),"console.info",ts(Ds,16,[4]),

1 个答案:

答案 0 :(得分:0)

您从GS文件加载HTML的模式是什么?

在SandboxMode.NATIVE中,SandboxMode.IFRAME中出现错误似乎有效

以下是GS文件的一些测试代码

function doGet(e) {
   var html= HtmlService.createTemplateFromFile("test.html");   
   return html.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

...和HTML作为test.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

   function myFunction() {

    if (jQuery) {  
     // jQuery is loaded  
       console.log( "Loaded" );
     } else {
     // jQuery is not loaded
      console.log( "Not Loaded" );
     }
     $("#message").html("Something happened.");
   }

   $(document).ready(myFunction);

</script>
<p id="message">Nothing happened. </p>