[英]Return an array of objects from the closest table row of checked checkboxes
我試圖獲取已選中復選框的表行的id值,但是由於某種原因。 沒有返回我當前腳本的對象數組:
console.log(array_checks);
返回我需要的:一個對象數組,並使腳本運行良好。 但是,如果我取消注釋.closest('tr').attr('id')
,它將不再返回對象數組,而是返回帶有第一個已檢查表行ID的字符串。 我在哪里弄錯
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的
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');
}
});
預期的輸出
console.log(array_checks);
-需要返回對象數組(row_132,row_91)
Object[
tr#row_132.checkbox-dash attribute value = "132",
tr#row_91.checkbox-dash attribute value = "91"
]
您可以使用has:
選擇器:
$('tr:has(.checkbox-dash:checked)')
要獲取所有ID,您不能使用attr
,因為它僅返回集合中第一個節點的值,但是您可以執行以下操作:
var ids = $('tr:has(.checkbox-dash:checked)').map(function () {
return this.id;
}).get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.