简体   繁体   English

onchange事件中数组中的push和pop项:javascript

[英]push and pop item in array on onchange event :javascript

I am working with the datatable, where data filled from api service. 我正在使用数据表,其中数据来自api服务。

I am adding checkboxes to eash row in action column. 我在操作列中添加了对清除行的复选框。

When i click checkbox is checked, the value of that specific record passed to javascript function using 'onchange' event. 当我单击复选框时,选中该特定记录的值,使用'onchange'事件传递给javascript函数。

Same with the uncheck function. 与uncheck功能相同。

Sample Script 示例脚本

//checkbox added to each row holding unique id(ignored mentioning variables)
var passingId,orgName;
var dataList=[];

<td><input type="checkbox" id=passingId onchange="checkThis(passingId + ',' + orgName )"/></td>

function checkThis(ids, oName) {
            var el = document.getElementById(ids);
            if (el.checked) {
                $.post("demo_test_post.asp",
                {
                    OId: ids,
                    ONAme: oName
                });
                var listItemAdd = oName;
                dataList.push(listItemAdd);
                alert(dataList);
            }
            else if (!el.checked) {
                $.post("demo_test_post.asp",
                {
                    OId: ids,
                    ONAme: oName
                });
                var listItemRemove = oName;
                dataList.pop(listItemRemove);
                alert(dataList);
            }
        }

This method push data to array when any record is check. 检查任何记录时,此方法将数据推送到数组。

When unchecked, the record of that row(oName only in this case) must be removed from array. 取消选中时,必须从数组中删除该行的记录(仅在此情况下为oName)。

Adding part has no problem, removing part is working but not removing selected value but removes last stored value. 添加零件没有问题,删除零件正在工作,但没有删除选定的值,但删除了最后存储的值。 I am surely missing something, as i am new to javascript can't understand the examples. 我肯定遗漏了一些东西,因为我是javascript的新手,无法理解这些例子。

If someone can help, please do help.Thanks for your time. 如果有人可以提供帮助,请帮忙。谢谢你的时间。

pop is a Stack operation, which removes last element of the array. pop是一个Stack操作,它删除了数组的最后一个元素。 Instead you should use something like search the given element and remove it, as shown below 相反,您应该使用类似搜索给定元素的内容并将其删除,如下所示

var listItemRemove = oName;
var index = dataList.indexOf(listItemRemove);
if(index >= 0) dataList.splice(index, 1);

What you should do, is replace the pop to splice, in order to do so, you need to find the removed element index in the array. 你应该做的是将pop替换为splice,为了做到这一点,你需要在数组中找到被删除的元素索引。

function checkThis(ids, oName) {
            var el = document.getElementById(ids);
            if (el.checked) {
                $.post("demo_test_post.asp",
                {
                    OId: ids,
                    ONAme: oName
                });
                var listItemAdd = oName;
                dataList.push(listItemAdd);
                alert(dataList);
            }
            else if (!el.checked) {
                $.post("demo_test_post.asp",
                {
                    OId: ids,
                    ONAme: oName
                });
                var removeItemIndex = dataList.indexOf(oName);
                if (removeItemIndex != -1) dataList.splice(removeItemIndex, 1);  //remove at this index - remove 1
                alert(dataList);
            }
        }

You can read more about splice here 您可以在此处阅读有关拼接的更多信息

Array.pop() always removes the last element in the array. Array.pop()总是删除数组中的最后一个元素。 In order to remove a specific element you must search for it and remove it. 要删除特定元素,您必须搜索并删除它。 This can be done using Array.indexOf to find the index of the element and Array.splice to remove it. 这可以使用Array.indexOf来查找元素的索引,使用Array.splice来删除它。

var index = dataList.indexOf(listItemRemove);
if (index !== -1){
    dataList.splice(index, 1);
}

Alternatively you could use the Array.filter function to remove all elements matching the search item. 或者,您可以使用Array.filter函数删除与搜索项匹配的所有元素。

dataList = dataList.filter(function(element){
    return element !== listItemRemove;
});

http://jsfiddle.net/ve8puvk3/ http://jsfiddle.net/ve8puvk3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM