简体   繁体   English

从选中的复选框的最接近表行返回对象数组

[英]Return an array of objects from the closest table row of checked checkboxes

I am trying to get the id values of a table row of the checkbox that had been checked, but for some reason. 我试图获取已选中复选框的表行的id值,但是由于某种原因。 is not returning an array of objects my current script: 没有返回我当前脚本的对象数组:

console.log(array_checks); returns what I need: an array of objects and makes the script work well. 返回我需要的:一个对象数组,并使脚本运行良好。 But if I un-comment .closest('tr').attr('id') , it doesn't return anymore the array of objects, it returns a string with the first checked table row id. 但是,如果我取消注释.closest('tr').attr('id') ,它将不再返回对象数组,而是返回带有第一个已检查表行ID的字符串。 Where am I mistaking 我在哪里弄错

HTML HTML

<tr id="row_132" class="even">
    <td>
      <input id="52" class="checkbox-dash" type="checkbox" value="52" name="delete-dash[]">
    </td>
</tr>
<tr id="row_91" class="even">
    <td>
      <input id="54" class="checkbox-dash" type="checkbox" value="54" name="delete-dash[]">
    </td>
</tr>

Jquery jQuery的

var array_checks = $('.checkbox-dash:checked')/*.closest('tr').attr('id')*/;
console.log(array_checks);
if (array_checks.length) {
   // Getting values of the table row id of checked checkboxes
   var array_values = array_checks.map(function(){
        return this.value;
   }).get();
} else {
    var array_values = 0;
    $('#message').html('Please select at least one row in order to delete.').show(700);
}
$.ajax({
    type: 'post',
    url: '../lib/ajax.html',
    data: {
            action: 'delete_sites_history',
            user_id: user_id,
            array_values: array_values
          },
    beforeSend: function() {
        $("#result").html('Loading...');
    },
    complete: function(data) {
        $("#result").html('');
        $.each(array_values, function(index, rows) {
            console.log(rows);
            $('#row_'+rows).fadeOut(1000);
        });
    },
    error: function() {
        $("#result").html('There was an error');
    }               
});

EXPECTED OUTPUT 预期的输出

console.log(array_checks); - need to return of array of objects (row_132, row_91) -需要返回对象数组(row_132,row_91)

Object[
   tr#row_132.checkbox-dash attribute value = "132", 
   tr#row_91.checkbox-dash attribute value = "91"
]

You can use the has: selector: 您可以使用has:选择器:

$('tr:has(.checkbox-dash:checked)')

To get all the ids, you cannot use attr however, since that returns the value of the first node in the set only, but you can do something like: 要获取所有ID,您不能使用attr ,因为它仅返回集合中第一个节点的值,但是您可以执行以下操作:

var ids = $('tr:has(.checkbox-dash:checked)').map(function () {
    return this.id;
}).get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM