如何获取表中所有已检查的ID

时间:2015-03-24 08:11:27

标签: jquery

当我勾选所有复选框并clcik删除按钮时,为什么它没有取出ID?

但是当我在下拉过滤器

下选择somestatus时,同样的事情正在发挥作用

http://jsfiddle.net/cdkLkcdk/19/

这是我的代码

   $(document).on('click', '#deletebtn', function(event ) {
  var $checked = $('#tablecontent').find(":checkbox:checked");

      if (!$checked.length) 
      {
        alert('Need to select atlease one checkbox button');
        event.stopImmediatePropagation();
        event.stopPropagation();
     }             
     else 
     {
        var ids = [];
        $.each($checked, function(i,e)
        {
          var status = $(e).parent().parent().find('.label-status').text();            
         if ($(e).attr("id") != 'selectall' && status == $('#filterstatus').val()) 
         {
            ids.push($(e).attr("id"))
         }          
      });        
     alert(ids);    
   }
});

2 个答案:

答案 0 :(得分:1)

问题似乎出现在filterstatus,如果您没有选择任何条件未处理的话

$(document).on('click', '#deletebtn', function (event) {
    var $checked = $('#tablecontent').find(":checkbox:checked").not('#selectall');

    var fstatus = $('#filterstatus').val();
    var ids = $checked.map(function () {
        var status = $(this).parent().parent().find('.label-status').text().trim();
        return !fstatus || fstatus == status ? this.id : undefined;
    }).get();

    if (ids.length) {
        alert('Need to select atlease one checkbox button');
        event.stopImmediatePropagation();
        event.stopPropagation();
    } else {
        alert(ids);
    }
});

演示:Fiddle

答案 1 :(得分:1)

我检查了你的jsFiddle。问题发生在您进行的status == $('#filterstatus').val()比较中。

我不确定它出错的地方(检查状态值似乎是正确的),但我将此行更改为:

if ($(e).attr("id") != 'selectall' && !$(e).is(":hidden"))

这确实解决了这个问题。

简短的解释:我不会过滤所选的值,而只采用可见的元素(="不隐藏",因为只有那些状态正确的元素在那一点可见)

相关问题