gpt4 book ai didi

javascript - 如何在函数外获取数组?

转载 作者:行者123 更新时间:2023-11-30 08:10:36 24 4
gpt4 key购买 nike

$("#div").click(function(){
$('#myselect').change(function() {
var names = [];
$('.checkbox input').each(function() {
if(this.value != ''){
names.push(this.value);
}
});
console.log(names);
});
});

我需要在该函数外使用 names 才能在另一个函数上使用它。谁能告诉我怎么做?

$("#dropdown").change(function(){
console.log(names);
});

names 是一个数组。

最佳答案

在函数外定义“名称”...

var names = [];

$('#myselect').change(function() {

$('.checkbox input').each(function() {
if(this.value != ''){
names.push(this.value);
}
});
console.log(names);
});

关于javascript - 如何在函数外获取数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10944876/

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