简体   繁体   English

Javascript-隐藏基于文本的动态Div

[英]Javascript - Hide Dynamic Div based on text

I have a table that creates dynamic Divs with ID's that enumerate as they are created similar to the below HTML: 我有一个表,该表创建具有与以下HTML相似的ID的动态Div。

<div id="dgpCheckDiv10_0">
    <input Delete
</div>

<div id="dgpCheckDiv10_1">
    text2
</div>

Certain Div's will have checkboxes that have the word 'Delete' as noted above and other Div's will have a checkbox that doesn't have a label at all. 某些Div的复选框具有如上所述的“删除”字样,而其他Div的复选框则根本没有标签。

I'm trying to write some javascript that will hide those checkboxes that don't contain the word 'Delete'. 我正在尝试编写一些JavaScript,以隐藏不包含“删除”一词的复选框。

I have the following script but it's not hitting the .hide function. 我有以下脚本,但未命中.hide函数。 Any ideas? 有任何想法吗?

$(document).ready(function () {
    var divs= document.getElementsByTagName('div');
    for (var i = 0, len = divs.length; i < len; ++i) {
        console.log(divs[i].id);
        //if(divs[i].id.substring(divs[i].id.length) == 'dgpCheckDiv10_*') {
        if(/^dgpCheckDiv10_/.test(divs[i].id)) {
            if(divs[i].innerHTML.indexOf(" Delete") === -1) {
                console.log(divs[i].innerHTML);
                $(divs[i]).hide();
                //$(this).closest('#divDGP2Container').find('#task').hide();
            }
        }
    }    
});

This will hide the DIVs that don't contain a checkbox whose value is Delete . 这将隐藏不包含值为Delete的复选框的DIV。

$("div[id^=dgpCheckDiv10_]:not(:contains(:checkbox[value=Delete]))").hide();

If you just want to hide the checkboxes, not the whole DIV: 如果只想隐藏复选框,而不是整个DIV:

$("div[id^=dgpCheckDiv10_] :checkbox:not([value=Delete])").hide();

This is what you need to do : 这是您需要做的:

<div id="dgpCheckDiv10_0">
 <input type="checkbox"/>Delete
</div>

<div id="dgpCheckDiv10_1">
<input type="checkbox"/>
</div>

$("div[id^='dgpCheckDiv10']").each(function()
{
    if($(this).html().indexOf("Delete") === -1)
    {
        $(this).find('input:checkbox').hide();
        // If you wish to remove it - just uncomment the following line
        //$(this).html("");
    }
});

http://jsfiddle.net/uj0twtd8/1/ http://jsfiddle.net/uj0twtd8/1/

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

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