gpt4 book ai didi

javascript - .withSuccessHandler(updateName) 未执行 $(document).ready 中的函数

转载 作者:行者123 更新时间:2023-12-02 17:10:09 24 4
gpt4 key购买 nike

我的代码基于开发人员的 example 。当 $(document).ready 被注释掉时,代码可以运行,但是当取消注释时,updateName 无法执行。 google.script.run.withSuccessHandler() 如何执行 $(document).ready 中的函数?

index.html

<form id="myForm">
<input name="myName" type="text" />
<input type="button" value="Submit"
onclick="google.script.run
.withSuccessHandler(updateName)
.processForm(this.parentNode)" />
</form>
<div id="output"></div>


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

<script>
<!-- $(document).ready(function(){ -->
function updateName(myName) {
var div = document.getElementById('output');
div.innerHTML = '<p> ' + myName + '</p>';
}

<!-- )} -->
</script>

代码.gs

function doGet() {
return HtmlService.createHtmlOutputFromFile('index');
}

function processForm(formObject) {
var myName = formObject.myName;
Logger.log(formObject.myName);
return myName
}

最佳答案

当您注释掉 $(document).ready 时,updateName() 被定义为全局范围。否则,它嵌套在 $(document).ready 中。

引用:Nested Functions and Closure

$(document).ready 的用途是包含您希望在页面加载并准备就绪时执行的操作。在您的示例中,不会发生任何事情,因为您拥有的只是一个函数定义,但没有任何东西可以调用它。

关于javascript - .withSuccessHandler(updateName) 未执行 $(document).ready 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24896802/

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