请查看下面的代码..
<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>
它不起作用。有没有更容易和有效的解决方案来获得这些ID。请帮忙。
谢谢。
答案 0 :(得分:2)
jQuery map函数是你的朋友
$.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(",");
将返回"block_1=item_1,item_2,item_3,block_2=item_4,item_5,item_6"
,这会让你更接近你想去的地方(我想)。
答案 1 :(得分:1)
我认为您在致电str += "block="+$(block).attr("id")+"&items=";
.attr无效,因为block是dom元素而不是jquery对象。你也有与var项目相同的问题。
这个javascript修复了它:
$("#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);
}
});
答案 2 :(得分:0)
$("#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
}
答案 3 :(得分:-1)
尝试没有“&gt;”
var result = $("#blocks_sortable div");