English 中文(简体)
选择在 jQuery 中带有选择器的元素
原标题:Select elements with selectors in jQuery

I have a nested list with checkboxes. With the first checkbox i want to toggle all the checkboxes in that list. I know how to toggle them with jQUery but i don t know how to select them. Thats the problem, the selecting part.

<ul>
    <li>
        <p> lorem ipsum </p>
        <ul class="list">
            <li><input type="checkbox"></li>
            <li><input type="checkbox"></li>
            <li><input type="checkbox"></li>
        </ul>
    </li>
    <li>
        <p> lorem ipsum </p>
        <ul class="list">
            <li><input type="checkbox"></li>
            <li><input type="checkbox"></li>
            <li><input type="checkbox"></li>
        </ul>
    </li>
 </ul>
最佳回答

假设我正确阅读了你的请求;

jjury < a href=" http://api.jquery.com/siblings/" rel=" no follow"\\\ code>.siblings ()

发件人:"http://jsfiddle.net/52DBv/"rel="no follow" >http://jsfiddle.net/52DBv/

问题回答

尝试此 :

$(".list").find("input[type=checkbox]:first").on("click", function(){
    var cb = $(this);
    cb.parents(".list").find("input[type=checkbox]").attr("checked", cb.is(":checked"));
});​

< 强势 > 编辑 : 抱歉, 现在为第二个列表工作 。

你可以用这样的东西去:

<% 1 > HTML :

<ul>
    <li>
        <p> lorem ipsum </p>
        <ul class="list">
            <li><input type="checkbox"></li>
            <li><input type="checkbox"></li>
            <li><input type="checkbox"></li>
        </ul>
    </li>
    <li>
        <p> lorem ipsum </p>
        <ul class="list">
            <li><input type="checkbox"></li>
            <li><input type="checkbox"></li>
            <li><input type="checkbox"></li>
        </ul>
    </li>
 </ul>​

< 强力 > 标注

$(".list li:first-child input[type=checkbox]").click(function() {
    if (!this.checked) {
        $(this).parents(".list").find("input[type=checkbox]").removeAttr(    "disabled");
    } else {
        $(this).parents(".list").find("input[type=checkbox]").attr(    "disabled", true);
        $(this).parents(".list").find("li:first-child input[type=checkbox]"    ).removeAttr("disabled");
    }
});​

you can see a live working demo here, or look at the jsFiddle. As so often is the case, there are plenty of possible solutions, but this could work.

您可以在包含类“列表”的元素内收集所有复选框如下:

$( .list input:checkbox )

所以,这样的事情应该有用:

    <ul>
        <li>
            <p> lorem ipsum </p>
            <ul class="list">
                <li><input type="checkbox" onclick="$( .list input:checkbox ).attr( checked , true);"></li>
                <li><input type="checkbox"></li>
                <li><input type="checkbox"></li>
            </ul>
        </li>
        <li>
            <p> lorem ipsum </p>
            <ul class="list">
                <li><input type="checkbox"></li>
                <li><input type="checkbox"></li>
                <li><input type="checkbox"></li>
            </ul>
        </li>
     </ul>

(您必须添加“ 切换” 逻辑)

检查 < a href=> "http://api.jquery.com/ checkbox-selector/" rel="nofollow" >jq 以获取更多信息, conjquery 复选框选择器。

尝试做到这一点 :

$( .list ).find( input:eq(0) ​)​.on( click , function(){        
    var $checkbox = $(this).is( :checked );
    $(this).closest( ul ).find( input ).attr( checked , $checkbox);
});​​​




相关问题
Finding a class within list

I have a class (Node) which has a property of SubNodes which is a List of the Node class I have a list of Nodes (of which each Node may or may not have a list of SubNodes within itself) I need to be ...

How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Is List<> better than DataSet for UI Layer in ASP.Net?

I want to get data from my data access layer into my business layer, then prepare it for use in my UI. So i wonder: is it better to read my data by DataReader and use it to fill a List<BLClasses&...

What is the benefit to using List<T> over IEnumerable<T>?

or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at ...

灵活性:在滚动之前显示错误的清单

我有一份清单,在你滚动之前没有显示任何物品,然后这些物品就显示。 是否有任何人知道如何解决这一问题? 我尝试了叫人名单。

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签