jquery筛选器未给出正确的结果

jquery筛选器未给出正确的结果,jquery,arrays,Jquery,Arrays,我有一个空的sendStatus[]。基本上,我是在用户单击复选框时检查数组中是否存在该值。如果没有,我只是用“add+this.value”添加值,以防用户再次单击复选框 我正在检查sendStatus[]是否包含“add+this.value”如果存在,我将删除它并添加一个新值作为“remove+this.value”,因此只有我才能在每个复选框中获得一个值作为“add+this.value”或“remove+this.value” 为此,我使用了此功能,但不起作用: var sendSta

我有一个空的
sendStatus[]
。基本上,我是在用户单击复选框时检查数组中是否存在该值。如果没有,我只是用“add+this.value”添加值,以防用户再次单击复选框

我正在检查
sendStatus[]
是否包含“add+this.value”如果存在,我将删除它并添加一个新值作为“remove+this.value”,因此只有我才能在每个复选框中获得一个值作为“add+this.value”或“remove+this.value”

为此,我使用了此功能,但不起作用:

var sendStatus = [];

var clearValues = function(newVal){
            console.log("calling",newVal);

        }

        $("form").on("click", ":checkbox", function(e){

            var el = $(this),parentClass = $(this).parent().hasClass("green"),label = $(this).parent();

            if(!parentClass){
                label.addClass("green");

                sendStatus = $.grep(sendStatus, function(value) {
                    return value != "remove" + el.val();
                });

                if ($.inArray("add" + el.val(), sendStatus) === -1) {
                    sendStatus.push("add" + el.val());
                }

            }else{
                label.toggleClass("red");

                sendStatus = $.grep(sendStatus, function(value) {
                    return value != "add" + el.val();
                });

                if ($.inArray("remove" + el.val(), sendStatus) === -1){
                    sendStatus.push("remove" + el.val());
                }
            }
        console.log(sendStatus);

        })  

任何人都可以简化我的功能,突出我所面临的问题。。提前感谢。

我很确定问题出在:复选框选择器中。console.log正在运行吗?无论如何,我认为更多的赋值将使代码更易读,减少出错的可能性。你可以试试这个例子

$("form").on("click",'input[type="checkbox"]', function(e){

            var el = $(this),
                label = el.parent(), 
                hasParentClass = label.hasClass("green"),
                elementValue = el.val(),
                removeElementValue = "remove"+ elementValue,
                addElementValue = "add"+ elementValue ;
            if(! hasParentClass ){
                label.removeClass("red").addClass("green");

                sendStatus = $.grep(sendStatus, function(value) {
                    return value != removeElementValue ;
                });

                if ($.inArray(addElementValue , sendStatus) === -1) {
                    sendStatus.push( addElementValue );
                }

            }else{
                label.removeClass("green").addClass("red");

                sendStatus = $.grep(sendStatus, function(value) {
                    return value != addElementValue ;
                });

                if ($.inArray(removeElementValue , sendStatus) === -1){
                    sendStatus.push( removeElementValue );
                }
            }
            console.log(sendStatus);

        }) ; 
注意()上的
.on
eventhandler, 用法:

在selector1部分中,您必须提供一个静态节点选择器,该选择器在javascript运行时不会更改(它来自webserver或静态地位于html文档中) 将选择器写在最接近二元数部分的位置。 e、 g:
$(“表格>正文>tr”)
selector2是selection的二进制部分,这些节点是在javascript运行时创建的,它们不在DOM中。 因此,请在此处填写您的复选框或td-s(如果您有,并且它们是按平日方式创建的)


请提供该结构的HTML示例:复选框不应该是“[type=“checkbox”]”无法正常工作,即使我再次检查该值(变为绿色),结果是:[“addAfrikaans”]admin.js:141[“removeAfrikaans”]admin.js:141[“removeAfrikaans”]admin.js:141[“removeAfrikaans”]抱歉,我几秒钟前修复了代码,看来我们没有改变课程,我只是用了你的更新版本。我认为问题就在这里:if(!hasParentClass){label.addClass(“绿色”);sendStatus=$.grep(sendStatus,function(value){return value!=removelementvalue;});if($.inArray(addElementValue,sendStatus)==-1){sendStatus.push(addElementValue);}}}100%你是对的。非常高兴你让我成为一名JSFIDLE。非常感谢你。你为我节省了一天时间!很高兴能提供帮助,如果你做两次或更多次相同的赋值,尝试在代码中使用更多变量,这对阅读很有帮助。
$("selector1").on("click",'selector2', function(e) {...
$("form table > tbody > tr").on("click",'td > input[type="checkbox"]', function(e){ ...