下面的代码,我得到tr
元素id
属性:获得TR元素的ID,如果该复选框被选中
var IDs = [];
$(".head").each(function(){ IDs.push(this.id); });
alert(IDs);
这些tr
元素都有复选框。
我想要的是,如果复选框被选中,那么我有这些tr
id。我需要检查复选框tr
ID :)
我该如何实现它?
下面的代码,我得到tr
元素id
属性:获得TR元素的ID,如果该复选框被选中
var IDs = [];
$(".head").each(function(){ IDs.push(this.id); });
alert(IDs);
这些tr
元素都有复选框。
我想要的是,如果复选框被选中,那么我有这些tr
id。我需要检查复选框tr
ID :)
我该如何实现它?
你需要用它来获得勾选复选框的父ID ...
var IDs = [];
$(".head input:checked").each(function(){ IDs.push($(this).parent().attr("id")); });
alert(IDs);
这里的工作的例子...
花花公子,$('#'+ Ids).each('slow',function(){slide}();这不起作用可能是什么问题? – snnlankrdsm 2012-04-17 11:17:09
这是一个完全不同的问题 - 创建一个新问题并发布完整的代码来概述问题,最好是链接到jsfiddle之类的东西来向我们展示。 – Archer 2012-04-17 11:41:00
像
var IDs = [];
//iterate over your <tr>
$(".head").each(function(){
//if there is atleas a checked checkbox
if($('input:checkbox:checked', this).length > 0){
//add the id of the <tr>
IDs.push(this.id);
}
});
alert(IDs);
,或者你可以做
$(".head input:checkbox:checked").each(function(){
IDs.push(this.id);
});
IDs是空的哪里出错:S – snnlankrdsm 2012-04-17 11:10:23
@MertMetin有一个错字,我认为(长度而不是长度) – 2012-04-17 11:12:24
$('#'+ Ids).each('slow',function ) { })。向上滑动();这不起作用可能是什么问题? - – snnlankrdsm 2012-04-17 11:26:42
你可以做到这一点是这样的...
var Ids = $('.head:has(:checkbox:checked)')
.map(function() { return this.id })
.get();
如果你想让它具有jQuery的执行速度更快内部杠杆querySelectorAll()
,您可以使用...
var Ids = $('.head').filter(function() {
return $(this).has('input[type="checkbox"]') && this.checked;
});
...得到一个包含复选框的.head
元素的jQuery集合。在你接受比男人
哥哥的工作!欢呼声:) – 2012-04-17 11:07:40
你的意思是说你的'.head'元素是复选框,并且你想获得他们父'tr'的'id'? – 2012-04-17 11:08:35
.head是tr的类和tr复选框 – snnlankrdsm 2012-04-17 11:09:13