JQuery - 使用之前声明的变量作为点击函数
问题描述:
已搜索答案很高而没有运气。JQuery - 使用之前声明的变量作为点击函数
我想在两个函数之间传递一个变量。第一个函数在页面加载时运行,从谷歌脚本文件中提取设置。这部分我没有任何问题。然而,我正在努力使用第二个函数,它运行'点击'并需要第一个函数的一些变量。
任何指导表示赞赏。
<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>
答
我假设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.
})
定义变量,你想要哪个变量再次使用全球 – Ritz
? – Ritz
@Ritz我想重复使用settings.userInput1。我试着定义一个全局变量,但是我尝试过的任何东西似乎都不起作用。 – Johnny