【问题标题】:Code returns all the items in Checkboxes代码返回 Checkboxes 中的所有项目
【发布时间】:2016-05-31 07:16:45
【问题描述】:

在这里我想获取 allRefItems 但它应该被检查而不是禁用。但是在这里总是得到 所有ID的

var allRefItems = [];
$('table#reftable > tbody > tr').each(function () {
    if ($(this).find('td:eq(0) input', this).is(':checked')) {
        if ($(this).find('td:eq(0) input', this).not(':disabled')) {
            itId = $(this).find('td:eq(0) input', this).attr('id'); 
            allRefItems.push(itId);
        }
    }
});

【问题讨论】:

  • 请添加标记,以便我们轻松复制。
  • 你需要if ($(this).find('td:eq(0) input').is(':not(:disabled)')) {或更好,只需使用$("input[type='checkbox']:checked:not(:disabled)")选择你想要的元素

标签: javascript jquery arrays asp.net-mvc


【解决方案1】:

如果您想获取表格中的所有复选框,我认为您可以更轻松:

var allRefItems = [];
$('table#reftable > tbody input[type="checkbox"]') //get all checboxes
   .filter(function() { // filter them only checked and not disabled
      return !this.disabled && this.checked;
   }).each(function () { //getting your ids
      itId = $(this).attr('id'); 
      allRefItems.push(itId);
});

这是jsFiddle example

【讨论】:

    【解决方案2】:

    你可以使用,

    var allRefItems = $('table#reftable > tbody > tr input[type="checkbox"]:checked:not(:disabled)').map(function() {
      return this.id;
    }).get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 2017-12-31
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      相关资源
      最近更新 更多