gpt4 book ai didi

javascript - 如何在切换时将 id 值存储在数组中?

转载 作者:行者123 更新时间:2023-11-30 06:49:11 26 4
gpt4 key购买 nike

我正在切换 'tr.subCategory1' 及其兄弟 .RegText。同时,我试图将它的 ID 存储在这样的数组中 list_Visible_Ids[$(this).attr('id')] = $(this).css('display') != 'none' ? 1 : null; (当我折叠时,我需要将 'null' 存储在数组中的 id 位置,如果我展开我需要存储,我需要将 1 存储在它的 id 位置)。但是每次 alert($(this).css('display')) 显示 block。我该如何处理?因此,当我折叠或展开时,它仅存储 1

$(document).ready(function() {

$('tr[@class^=RegText]').hide().children('td');

list_Visible_Ids = [];
var idsString, idsArray;

idsString = $('#myVisibleRows').val();
idsArray = idsString.split(',');

$.each(idsArray, function() {
if (this != "" || this != null) {
$('#' + this).siblings('.RegText').toggle();
list_Visible_Ids[this] = 1;
}
});
$('tr.subCategory1')
.css("cursor", "pointer")
.attr("title", "Click to expand/collapse")
.click(function() {
$(this).siblings('.RegText').toggle();
$(this).siblings('.VolumeRegText').toggle();
//alert($(this).css('display'))
list_Visible_Ids[$(this).attr('id')] = $(this).css('display') != 'none' ? 1 : null;

});

$('#form1').submit(function() {

idsString = '';
for (var index in list_Visible_Ids) {
idsString += (idsString != '' ? ',' : '') + index;
}

$('#myVisibleRows').val(idsString);
form1.submit();
});
});

最佳答案

您没有切换 tr 本身,只有它的兄弟(类 .RegText.VolumeRegText)。因此,当您将状态存储在数组中时,您必须检查这些是否可见。为此,您可以在 sibling 之一上使用 .is(":hidden")。点击函数看起来像这样

$('tr.subCategory1')
.css("cursor", "pointer")
.attr("title", "Click to expand/collapse")
.click(function() {
$(this).siblings('.RegText').toggle();
var isHidden = $(this).siblings('.VolumeRegText').toggle().is(':hidden');
list_Visible_Ids[$(this).attr('id')] = !isHidden ? 1 : null;
});

其余代码也有很多要注释的地方。在

$('tr[@class^=RegText]').hide().children('td');

跳过 .children('td'),因为您没有使用此选择。


list_Visible_Ids = []; 应使用 var 声明。


遍历 idsArray,您正在检查 this != ""|| this != null),应该使用 && 而不是 ||

$.each(idsArray, function() {
if (this != "" && this != null) {
$('#' + this).siblings('.RegText').toggle();
list_Visible_Ids[this] = 1;
}
});

使用 $.each() 函数代替常规的 JavaScript for 循环也毫无意义


似乎您使用的是旧版本的 jQuery,因为在 version 1.3 中不推荐在选择器中使用 @ .

关于javascript - 如何在切换时将 <TR> id 值存储在数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2845631/

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