Javascript JQuery-使用以前声明的变量作为单击函数
没有运气就到处寻找答案 我试图在两个函数之间传递一个变量。第一个函数在页面加载时运行,从google脚本文件中提取设置。我对这部分没有任何问题。但是,我正在努力处理第二个函数,该函数在“单击”时运行,并且需要来自第一个函数的一些变量 感谢您的指导Javascript JQuery-使用以前声明的变量作为单击函数,javascript,jquery,html,google-apps-script,Javascript,Jquery,Html,Google Apps Script,没有运气就到处寻找答案 我试图在两个函数之间传递一个变量。第一个函数在页面加载时运行,从google脚本文件中提取设置。我对这部分没有任何问题。但是,我正在努力处理第二个函数,该函数在“单击”时运行,并且需要来自第一个函数的一些变量 感谢您的指导 <script> $(function() { google.script.run .withSuccessHandler(loadSettings) .withFail
<script>
$(function() {
google.script.run
.withSuccessHandler(loadSettings)
.withFailureHandler(showStatus)
.withUserObject($('#button-bar').get())
.getSettings();
function loadSettings(settings) {
$("select").data("option",settings.userInput1);
};
$('#add1').click(function(){
var option0 = $("select").data("option");}
...etc.
})
</script>
如果其他解决方案不起作用,那么您应该这样做- 做一个隐藏的字段 将该变量值存储在该隐藏字段中 在下一个函数中,从该字段获取值 $hiddenfield_id.val;
如果其他解决方案不起作用,那么您应该这样做- 做一个隐藏的字段 将该变量值存储在该隐藏字段中 在下一个函数中,从该字段获取值 $hiddenfield_id.val; 我假设settings.userInput1是一个字符串。试试下面的方法
<script>
var userInput ="";
$(function() {
google.script.run
.withSuccessHandler(loadSettings)
.withFailureHandler(showStatus)
.withUserObject($('#button-bar').get())
.getSettings();
function loadSettings(settings) {
userInput = settings.userInput1;
$("select").data("option",settings.userInput1);
};
$('#add1').click(function(){
//you can now use userInput in this function
var option0 = $("select").data("option");}
...etc.
})
我假设settings.userInput1是一个字符串。试试下面的方法
<script>
var userInput ="";
$(function() {
google.script.run
.withSuccessHandler(loadSettings)
.withFailureHandler(showStatus)
.withUserObject($('#button-bar').get())
.getSettings();
function loadSettings(settings) {
userInput = settings.userInput1;
$("select").data("option",settings.userInput1);
};
$('#add1').click(function(){
//you can now use userInput in this function
var option0 = $("select").data("option");}
...etc.
})
将变量定义为globalWhat variable要再次使用?@Ritz我正在尝试重用settings.userInput1。我试图定义一个全局变量,但无论我尝试了什么,似乎都不起作用。请将该变量定义为globalWhich variable您想再次使用哪个变量?@Ritz我正在尝试重用settings.userInput1。我试着定义一个全局变量,但不管我做了什么,似乎都不管用。谢谢。非常有创意的方法。谢谢。非常有创意的方法。不幸的是,这不起作用。我又试了一次。我被难倒了为什么不呢!尝试添加console.log并检查该变量的类型谢谢@Ritz。非常感谢。我在脚本中遇到了一个错误,它阻止了全局变量的实际工作。你上面的解决方案是正确的。再次感谢。不幸的是,这不起作用。我又试了一次。我被难倒了为什么不呢!尝试添加console.log并检查该变量的类型谢谢@Ritz。非常感谢。我在脚本中遇到了一个错误,它阻止了全局变量的实际工作。你上面的解决方案是正确的。再次感谢。