如果span包含“(0)”,则隐藏父元素

时间:2013-04-04 19:27:07

标签: jquery

HTML

<div>Group 1
    <br>

        <input type="checkbox" name="testing" value="B"> <label>A<span>(34)</span></label>
    <br />
        <input type="checkbox" name="testing" value="I"> <label>B<span>(0)</span></label>
    <br />
        <input type="checkbox" name="testing" value="A"> <label>C<span>(323)</span></label>
    <br />
</div>
<div>Group 2
    <br>

        <input type="checkbox" name="testing2" value="B"><label>A<span>(0)</span></label>
    <br />

        <input type="checkbox" name="testing2" value="I"><label>B<span>(564)</span></label>
    <br />

        <input type="checkbox" name="testing2" value="A"> <label>C<span>(0)</span></label>
    <br />
</div>

JS

$("input:checkbox").change(function () {
    var checkname = $(this).attr("name");

    if (this.checked) {

        $("input:checkbox[name='" + checkname + "']").removeAttr("checked").parent().hide();
        this.checked = true;
        $(this).parent().show();
    } else {
        $("input:checkbox[name='" + checkname + "']").parent().show();
    }

    if($(".options input:checkbox + label span:contains('(0)')")){
        $(this).parent().hide();;
    }else{
        $(this).parent().show();
    }
});


$("input:checkbox[checked]").trigger("change"); 

如果子跨度包含(0),我想隐藏带有标签的复选框。你可以看到我上面尝试过但它不起作用。怎么做?

http://jsfiddle.net/warface/uvYzW/4/

2 个答案:

答案 0 :(得分:1)

标记:

<div>Group 1
    <div>
        <input type="checkbox" name="testing" value="B"> <label>A<span>(34)</span></label>
    </div>
    <div>
        <input type="checkbox" name="testing" value="I"> <label>B<span>(0)</span></label>
    </div>
</div>

JS:

$("label span:contains('(0)')").closest("div").hide();

答案 1 :(得分:0)

试试这个:

$.each( $("input[type=checkbox]") , function(key, value) {  
    var element = $(value);
    if (element.next().find("span").text()==="(0)") {               
        element.prev().hide();  // remove the <br>
        element.hide();
        element.next().hide(); // remove the label
    }   
});

http://jsfiddle.net/Hgg58/2/