Retrieving ids of chid divs using jquery?

2019-09-06 13:02发布

问题:

Please have a look on the code below ..

                <div id="click_me">Save</div> 


<div id="blocks_sortable">
    <div id="block_1">
        <h2>Block 1</h2>

        <div class="items_sortable connectedSortable">
            <div id="item_1"> 
                <span>Item 1</span></div>   
            <div id="item_2"> 
                <span>Item 2</span></div>
            <div id="item_3"> 
                <span>Item 3</span></div>
        </div>
    </div>  
    <div id="block_2">
        <h2>Block 2</h2>

        <div class="items_sortable connectedSortable">
            <div id="item_4"> 
                <span>Item 4</span></div>   
            <div id="item_5"> 
                <span>Item 5</span></div>
            <div id="item_6"> 
                <span>Item 6</span></div>
        </div>
    </div>
</div>

<script>
    $("#click_me").click(function() {
            var result = $("#blocks_sortable > div");

            for(var i=0; i<result.length; i++){
                var str = ""; 
                var str2 = "";
                var block = result[i]; //div object

                //retrieve block id and create the string with id
                str += "block="+$(block).attr("id")+"&items=";

                //trying to select all the items of the current Block
                var result2 = $(block+" > div");                        
                    for(var j=0; j<result2.length; j++){
                        var item = result2[j];

                        str2 += $(item).attr("id")+",";
                    } //end for items

                str = str+str2;
                // looking for a final string in the format of .. block=block_1&items=item_1,item_2,item_3 for loop 1
                alert(str);
            }
    });
</script>

It is not working. Is there any easier and working solution to get these ids. Please help.

Thank you.

回答1:

jQuery map function is your friend

$.map( 
    $("#blocks_sortable > div"),
    function(elementOfArray){
        var block = $(elementOfArray);
        var children = $.map(
            $("div[id]", block),
            function( eoa ){
                return eoa.id;
            }).join(",");

        return elementOfArray.id + "=" + children;
    }
).join(",");

Will return "block_1=item_1,item_2,item_3,block_2=item_4,item_5,item_6", which gets you much closer to where you're going (I think).

EDIT: Solution and source here.



回答2:

I think you are getting an error when you call str += "block="+$(block).attr("id")+"&items=";

.attr is not working because block is a dom element and not a jquery object. You are having the same issues with var item too.

The working solution is here. And the demo is here.

This javascript fixes it:

   $("#click_me").click(function() {
            var result = $("#blocks_sortable > div");

            for(var i=0; i<result.length; i++){
                var str = "";
                var str2 = "";
                var block = result[i]; //div object

                //retrieve block id and create the string with id
                str += "block="+ block.id +"&items=";

                //trying to select all the items of the current Block
                var result2 = $("#" + block.id + "> div > div");
                    for(var j=0; j<result2.length; j++){
                        var item = result2[j];

                        str2 += item.id +",";
                    } //end for items

                str = str+str2;
                // looking for a final string in the format of .. block=block_1&items=item_1,item_2,item_3 for loop 1
                alert(str);
            }
    });


回答3:

    $("#click_me").click(function() {
        var result = $("#blocks_sortable > div"); 

        for(var i=0; i<result.length; i++){
            var str = ""; 
            var str2 = "";
            var block = result[i].id; //div object

            //retrieve block id and create the string with id
            //str += "block="+$(block).attr("id")+"&items=";
            str += "block="+ block + "&";
            //trying to select all the items of the current Block
            var inner_span = "#" + block + " > div";
            var result2 = $(inner_span).children();

                for(var j=0; j<result2.length; j++){
                    var item = result2[j];

                    str2 += $(item).attr("id")+",";
                } //end for items

            str = str+str2;
            // looking for a final string in the format of .. block=block_1&items=item_1,item_2,item_3 for loop 1
            alert(str); // output block=block_1&item_1,item_2,item_3 
                        //        block=block_2&item_4,item_5,item_6

        }


回答4:

try without ">"

var result = $("#blocks_sortable div");