Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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 jquery中为动态添加的行查找类?_Javascript_Jquery - Fatal编程技术网

Javascript 在tr jquery中为动态添加的行查找类?

Javascript 在tr jquery中为动态添加的行查找类?,javascript,jquery,Javascript,Jquery,我需要找到tr中使用的类。这些tr是动态添加的。我正在使用下面的代码来实现这一点,但它没有按预期工作。有人能告诉我为什么它不起作用吗 if (!($("tr:has(td.comboselected)").length > 0)) { alert('if'); $(this).addClass("comboselected"); } else { alert('else'); $(this).removeClass("

我需要找到tr中使用的类。这些tr是动态添加的。我正在使用下面的代码来实现这一点,但它没有按预期工作。有人能告诉我为什么它不起作用吗

if (!($("tr:has(td.comboselected)").length > 0)) {
        alert('if');
        $(this).addClass("comboselected");
    } else {
        alert('else');
        $(this).removeClass("comboselected");
    }
提前感谢…

您可以使用。在
tr
中选择类
组合来查找
tds

if ($("tr td.comboselected").length > 0) {
    alert('if');
    $(this).addClass("comboselected");
} else {
    alert('else');
    $(this).removeClass("comboselected");
}
写:


这对你很有帮助

试试这样的方法

对于在
td

    if($("tr td.comboselected").length){
        $(this).removeClass("comboselected");
    }else{
        $(this).addClass("comboselected");
    }
对于在
tr

    if($("tr.comboselected").length){
        $(this).removeClass("comboselected");
    }else{
        $(this).addClass("comboselected");
    }

您需要在该表的所有tr中应用jquery每个循环,如下所示:

方式1:如果在动态创建每个tr时为其分配唯一Id。

  $("#yourTableId tr").each(function (index) {
   // index is the current row index.
   // find the id of current tr
   var id = this.id;
   var currentClass = $("#"+id).attr("class");     
   // currentClass is the class you need to find.....
  });
方式2:如果您没有为动态创建的tr分配任何id

  $("#yourTableId tr").each(function (index) {
   // index is the current row index.
   var currentClass = $(this).attr("class");     
   // currentClass is the class you need to find.....
  });
谢谢

jQuery(document).ready(function () {
            if (!($("tr td").hasClass("comboselected"))) {
                $("tr td").addClass("comboselected");
            } else {
                alert('else');
                $("tr td").removeClass("comboselected");
            }
        });
正是你想要的
这是代码

嘿,我对你的代码做了更改

检查一下

$("#deviceInterfacetbl").on("click", "tr", function (event) {                    
                var checkbox = $(this).find("input[type='checkbox']");
                if (!checkbox.prop('checked')) {
                    $(this).addClass("comboselected");
                    var id = checkbox.val(); $('#policyMaptbl').empty();
                    $('#classMaptbl').empty();
                }
                else {
                    $(this).removeClass("comboselected");
                }
            });

选择器不关心元素是静态的还是动态的,它们只在当前DOM上操作。如果方法
$(“#DeviceInterfaceBl”)中存在条件,则在上面。在(“单击”,“tr”,函数(事件){…}
。这是tr。它总是返回false。我将not添加到if条件中。您能与我们共享您拥有的html吗?
$(“#deviceinterfacebl”)。在(“单击”,“tr”,函数(事件){var checkbox=$(this.find”(“输入[type='checkbox']);var tr=$(this.find”(“tr”);if(!($(tr td.comboselected”).length>0{alert('if');$(this.addClass(“comboselected”);}else{alert('else');$(this.removeClass)(“comboselected”)}event.stopImmediatePropagation();});
这是javascript,我希望生成的HTMLC可以告诉你在html中检查什么,因为它很大,包含许多css类和jstl代码。在这段代码中,它将找到'tr'标记,在'td'标记之后,它将找到类'comboselected',因为在jquery中有一个内置函数。hasClass将找到'td'标记编辑你的答案以添加解释,而不仅仅是注释;此外,你的答案似乎是向后的-如果标记已经有类,它似乎是在添加类。我的函数如下。我有tr在我有复选框内,我正在使用jquery上的“on”方法来更新tr,如果我们单击tr,它将起作用e、 但是如果我们点击复选框,那么会触发两个事件,因为它不起作用。我尝试停止传播并返回false,以触发一个事件。但它不起作用。
$(“#DeviceInterfaceBl”)。在(“点击更改”,“tr”,函数(事件){var checkbox=$(this)。find(“输入[type='checkbox']);var tr=$(this)。find(“tr”);if(!checkbox.is(“:checked”){checkbox.prop('checked',true);$(this.addClass('comboselected”);var id=checkbox.val();$('policyMaptbl').empty();$)$(checkbox.prop('checked',false);$(this.removeClass('comboselected')}event.stopImmediatePropagation();event.stopPropagation();返回false;});
jQuery(document).ready(function () {
            if (!($("tr td").hasClass("comboselected"))) {
                $("tr td").addClass("comboselected");
            } else {
                alert('else');
                $("tr td").removeClass("comboselected");
            }
        });
$("#deviceInterfacetbl").on("click", "tr", function (event) {                    
                var checkbox = $(this).find("input[type='checkbox']");
                if (!checkbox.prop('checked')) {
                    $(this).addClass("comboselected");
                    var id = checkbox.val(); $('#policyMaptbl').empty();
                    $('#classMaptbl').empty();
                }
                else {
                    $(this).removeClass("comboselected");
                }
            });