Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 比较前后的表tr值,得到两个不同的对象或数组_Javascript_Jquery_Css - Fatal编程技术网

Javascript 比较前后的表tr值,得到两个不同的对象或数组

Javascript 比较前后的表tr值,得到两个不同的对象或数组,javascript,jquery,css,Javascript,Jquery,Css,我有一个这样的桌子结构 <table id="dltable" class="dTable"> <tbody> <tr class="dRow " data-userid="s-1-5-21-34154-1668"> <td class="dCell" style="width:40px;"> <img src="1.png" class="avatar" /&g

我有一个这样的桌子结构

<table id="dltable" class="dTable">
    <tbody>
        <tr class="dRow " data-userid="s-1-5-21-34154-1668">
            <td class="dCell" style="width:40px;">
                <img src="1.png" class="avatar" />
            </td>
            <td class="dCell" style="vertical-align: top; padding-top:8px;"><span class="personItem">person 1</span>

            </td>
            <td class="dCell" style="width:30px;">
                <div class="dir-list_tick on hide" onclick="toggleSelection(this);" style="display: block;"></div>
            </td>
        </tr>
        <tr class="dRow " data-userid="s-1-5-21-3415-3433">
            <td class="dCell" style="width:40px;">
                <img src="2.png" class="avatar" />
            </td>
            <td class="dCell" style="vertical-align: top; padding-top:8px;"><span class="personItem">person 2</span>

            </td>
            <td class="dCell" style="width:30px;">
                <div class="dir-list_tick on hide" onclick="toggleSelection(this);" style="display: block;"></div>
            </td>
        </tr>
    </tbody>
</table>  
和CSS

.dir-list_tick.on {
    background-image: url(/_layouts/15/images/arrow.png);
    background-position: 50%;
    width:22px;
    height:22px;
}

.dir-list_tick.off {
    background-image: url(/_layouts/15/images/arrow_inactive.png);
    background-position: 50%;
    width:22px;
    height:22px;
}
现在我想要的是,当用户单击编辑按钮时,我捕获所有具有这些ID和状态的用户(至少现在是这样)

然后,当用户单击另一个按钮“保存”时,我想获取所有状态(将来可能有更多属性)已从“开”更改为“关”或“关”更改为“开”的用户

我能想到的唯一方法就是一遍又一遍地遍历这个表来比较它们,有没有更简单的方法

编辑

    var beforeEditUsers = [];
    $('#dltable > tbody  > tr').each(function () {
        beforeEditUsers.push({
              id : $(this).attr('data-userid'),
              status : $(this).children('.dir-list_tick').hasClass('on')
        });
    });
根据评论,我认为我走的是对的,但我被困在这里

    var beforeEditUsers = [];
    $('#dltable > tbody  > tr').each(function () {
        beforeEditUsers.push({
              id : $(this).attr('data-userid'),
              status :  // how can i cast div with class dir-list_tick on class as one here and otherwise 0 ?
        });
    });
找到了它

    var beforeEditUsers = [];
    $('#dltable > tbody  > tr').each(function () {
        beforeEditUsers.push({
              id : $(this).attr('data-userid'),
              status : $(this).children('.dir-list_tick').hasClass('on')
        });
    });

“我能想到的唯一方法是一次又一次地遍历表来比较它们”我不太确定您想要实现什么,但从听起来,您可以通过为用户编辑的行指定
dirty
标志来简化它。单击“保存”按钮后,您可以选择这些行,并对它们执行所需操作。@RoryMcCrossan谢谢,也许您是对的,但这样我就不知道用户是否真的更改了值或只是切换了值,必须有办法解决这一问题,但由于我可能需要为此tr添加更多属性,因此我将继续比较对象,请看编辑后的问题,非常感谢
    var beforeEditUsers = [];
    $('#dltable > tbody  > tr').each(function () {
        beforeEditUsers.push({
              id : $(this).attr('data-userid'),
              status : $(this).children('.dir-list_tick').hasClass('on')
        });
    });