当从jquery的点击事件中的每个循环创建的数组到它之外时,它显示空白数组
It showing blank array when array created from each loop in click event of jquery to outside of it
tmcartepo = [];
$('input[name^="tm_option_"]').click(function () {
if ($(this).is(':checked')) {
$('input[name^="tm_option_"]:checked').each(function(){
tmcartepo.push($(this).val());
console.log(tmcartepo);
});
}
}(;
console.log(tmcartepo);
这是我的代码,我正在尝试将复选框的值获取到数组中并通过 ajax 将其返回到另一个页面。但是当我将该数组打印到单击方法的范围时,它工作正常,但在它之外,它显示空白数组。当我尝试控制台.log(tmcartepo(时,它显示空白数组
提前致谢
您可能需要在单击方法的范围之外声明变量tmcartepo
(全局(。
var tmcartepo;
$('input[name^="tm_option_"]').click(function () {
if ($(this).is(':checked')) {
$('input[name^="tm_option_"]:checked').each(function(){
tmcartepo.push($(this).val());
});
console.log(tmcartepo);
alert(tmcartepo);
}
});
console.log(tmcartepo);
// declare variable in global scope
var tmcartepo=[];
$('input[name^="tm_option_"]').click(function () {
if ($(this).is(':checked')) {
$('input[name^="tm_option_"]:checked').each(function(){
tmcartepo.push($(this).val());
});
console.log(tmcartepo);
alert(tmcartepo);
} });
相关文章:
- 如何在映射数组中添加换行符
- javascript结合了数组和字典
- 当从jquery的点击事件中的每个循环创建的数组到它之外时,它显示空白数组
- 如何删除javascript数组中的空白元素
- 如何将项追加到空白数组中
- Php和javascript数组-跳过空白值并添加到新数组中
- 使用jQuery.parseJSON检索包含空白的php数组
- 数组未设置回[](空白)
- 为什么't trim()不能处理$ ?用于删除字符串数组中的空白
- 排序javascript数组,使空白值总是在底部
- 将数组转换为函数参数的空白变量列表
- 反转数组并使用javascript填充空白
- 去掉JS数组并保留空白
- Web Audio API分析器节点getByteFrequencyData返回空白数组
- 拼接数组中剩余的最后一个空白
- 如何以正确的顺序创建数组对象,不存在的键值映射为空白值
- 如何从自动完成数据中删除空白数组
- 被记录的数组在返回时显示为空白
- Js数组到json转换,结果是空白页
- 清除字符串数组中的空白