简体   繁体   English

从Javascript数组中删除动态添加的变量

[英]Remove dynamically added variables from Javascript array

I'm working on a project where I generate some checkboxes, with the value based on PHP variables, like this: 我正在开发一个项目,该项目会生成一些复选框,其值基于PHP变量,如下所示:

<input type="checkbox" value="<?php echo $example; ?>">

I then want to be able to grab the value of a clicked input and add it to a JS array, so that I can post all of the values from any checked checkboxes. 然后,我希望能够获取单击的输入的值并将其添加到JS数组中,以便我可以从任何选中的复选框发布所有值。

The values are adding to my JS array no problem, but when I try to remove them from the array I'm running into difficulties. 这些值添加到我的JS数组中没问题,但是当我尝试从数组中删除它们时,我遇到了麻烦。 If there is an individual element in the array then it will be removed fine, if there are more than that then my code only seems to remove the most recently added. 如果数组中有一个单独的元素,则将其删除,如果有更多的元素,则我的代码似乎仅删除最近添加的元素。

I think the problem is from the value_array.splice(index, 1); 我认为问题出在value_array.splice(index, 1); line. 线。

$(document).on('click', '.example-input', function() {

    input = $(this).find('input');

    if(example_condition == true) {
        $(input).prop( 'checked', true );
        value = $(input).val();
        value_array.push(value);
        index = value_array.indexOf(value);
        is_checked = true;
    } else {
        $(input).prop('checked', false);
        is_checked = false;
    }

    if(is_checked == false) {
        if(index !== -1) {
            value_array.splice(index, 1);
        }
    }

    // Post JS array
});

Hopefully, someone can point me in the right direction here. 希望有人可以在这里指出正确的方向。

I've also tried implementing the answer that was given here: How do I remove a particular element from an array in JavaScript? 我还尝试实现此处给出的答案: 如何从JavaScript中的数组中删除特定元素? , but it does not seem to work when there are multiple elements in the array. ,但是当数组中有多个元素时,它似乎不起作用。

Thanks. 谢谢。

Here's how I would tackle this: create a function that iterates over the checked boxes and adds to the array, call it when the page loads and when anything changes 这是我要解决的方法:创建一个遍历复选框并添加到数组的函数,在页面加载以及任何更改时调用它

 var value_array; $(document).ready(function() { updateValueArray(); $(document).on('change', '.myCheck', function() { updateValueArray(); console.log(value_array); }); }); function updateValueArray() { value_array = []; $('.myCheck:checked').each(function() { value_array.push($(this).attr("value")); }); } 
 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> </head> <body> <input class="myCheck" type="checkbox" value="1"> <input class="myCheck" type="checkbox" value="2"> <input class="myCheck" type="checkbox" value="3"> <input class="myCheck" type="checkbox" value="4"> <input class="myCheck" type="checkbox" value="5"> <input class="myCheck" type="checkbox" value="6"> <input class="myCheck" type="checkbox" value="7"> </body> </html> 

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

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