简体   繁体   English

从数组中删除一个值

[英]removing a value from array

I am working on a dropdown list which contains a checkbox and a value. 我正在处理一个包含复选框和值的下拉列表。 When i select a checkbox, the id of that perticular item is appended to a input on right panel and the value of that item gets appended in the form of a tag on the right panel. 当我选中一个复选框时,该垂直项目的ID将附加到右侧面板上的输入中,并且该项目的值将以标签的形式附加在右侧面板上。 When i click again on a checked checkbox, the appended value and the tag on the right have to be removed. 当我再次单击选中的复选框时,必须删除附加值和右侧的标签。

The problem i am facing is that the checkbox i am unchecking is not getting removed, instead some other alread appended value is getting removed. 我面临的问题是我取消选中的复选框没有被删除,而是其他一些已附加的值被删除了。

Javascript: 使用Javascript:

var arr = [];
$(".taglistarea input[type='text']").click(function(){
    if(!$(this).parent("div").next(".taglist").is(":visible")) {
        $(this).parent("div").next(".taglist").slideDown();
    } else {
        $(this).parent("div").next(".taglist").slideUp();
    }
});

$(".taglist ul li a input[type='checkbox']").click(function(){
    var tagidall = $(".taglist ul li a").attr("id");

    var tagideach = $(this).parent("a").attr("id");
    var tagtexteach = $(this).text();

    arr.push(tagideach);

    if($(this).is(":checked")) {
        $(this).addClass("active");
        $(".ss").append('<a href="#" id="'+tagideach+'" class="tagss">International<span class="closetag"></span></a>');    
    } else {
        $(".ss a[id="+tagideach+"]").remove();
        //arr.splice(tagideach,1);
    }
    //alert(arr);
    $("#idvals").val(arr.toString());
});

i think that problem is here: 我认为这个问题在这里:

arr.splice(tagideach,1);

the complete fiddle is here: https://jsfiddle.net/kue83ud8/ 完整的小提琴在这里: https : //jsfiddle.net/kue83ud8/

Thanks. 谢谢。

as you said, the problem is with arr.splice(tagideach,1); 如您所说,问题出在arr.splice(tagideach,1);

first argument of splice is the index to remove, not an object splice第一个参数是要删除的索引 ,而不是对象

you should replace that line with arr.splice(arr.indexOf(tagideach),1); 您应该用arr.splice(arr.indexOf(tagideach),1);替换该行arr.splice(arr.indexOf(tagideach),1); (you should also check if indexOf returns value other than -1 (您还应检查indexOf返回非-1

another remarks 另一句话

you should cache your selectors to improve performance: 您应该缓存选择器以提高性能:

if(!$(this).parent("div").next(".taglist").is(":visible")) {
    $(this).parent("div").next(".taglist").slideDown();
} else {
    $(this).parent("div").next(".taglist").slideUp();
}

should become: 应该变成:

var element = $(this).parent("div").next(".taglist");
if(!element.is(":visible")) {
    element.slideDown();
} else {
    element.slideUp();
}

instead of checking if element is visible you can use slideToggle() 而不是检查元素是否可见,可以使用slideToggle()

also, you should read about event delegation in jQuery 另外,您应该阅读有关jQuery中的事件委托的信息

设置要从数组中删除的元素索引

arr.splice(arr.indexOf(tagideach),1);

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

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