[英]Get Id of table row if it is checked?
I have a datatable containing a list of Cars. 我有一个包含汽车列表的数据表。 each row in the html contains a Car ID.
html中的每一行都包含一个汽车ID。 I have added checkbox column to the first cell in my datatable - if it is checked the row is highligted to indicate to the user they have selected that row.
我已将复选框列添加到数据表的第一个单元格-如果选中该行,则该行将向用户高亮指示已选择该行。 What I waht to do is get all the IDs of all the cars a user has selected on clicking a button on the page.
我要做的是获取用户单击页面上的按钮时选择的所有汽车的所有ID。 (also there are other columns in the table row where I have checkboxes (ie - a Manual column or an Automatic column which will somtime be checked - like in column 5 ot 6 in the table)
(在表行中还有其他列,其中有复选框(即-将手动检查的“手动”列或“自动”列-如表中的第5列或第6列)
so this is part of the cshtml for my page.. 所以这是我页面的cshtml的一部分。
@foreach (var car in Model.Cars)
{
<tr carId="@Html.DisplayFor(modelItem => car.CarID)">
<td class="table-data">
<input id="SelectIndividual" name="Select" type="checkbox" />
</td>
//more stuff set in other tds in table
Then this is the JS I have for the page so far. 这是到目前为止我在该页面上拥有的JS。
$('#GetSelectedCars').click(function (event) {
var cars= new Array();
carsListTable.find("tr").each(function (index, para) {
$('tr').find('td:input:checked:first').each(function () {
var car= new Object();
alert($(this).parent().parent().attr("carId"));
car.ID = $(this).parent().parent().attr("carId");
cars.push(car);
});
});
var jsonString = $.toJSON(cars);
I want to then return the json string to my controller (I do this by passing the value into a hidden field on my model and then deserialize - but at the minute I am getting it as empty. My problem is getting the best way to get the id from the row if it is checked? 然后我想将json字符串返回给我的控制器(我通过将值传递到模型上的隐藏字段中然后进行反序列化来完成此操作-但在此刻我将其视为空。我的问题是获得最佳方法该行的ID(如果已选中)?
You can use the selectors :checkbox :checked and use the jQuery.map to convert the array. 您可以使用选择器:checkbox :checked并使用jQuery.map转换数组。 The jQuery.closest() method will give the closest ancestor matching the given selector.
jQuery.closest()方法将提供与给定选择器匹配的最接近的祖先。
var cars = carsListTable.find('.table-data :checkbox:checked').map(function(i, v){
return {
ID : $(v).closest('tr').attr('carId')
}
});
Note: The id of elements should be unique in a document so the id
of the checkbox
should be removed or has to be suffixed or prefixed by a dynamic value like the car id. 注意:元素的ID在文档中应该是唯一的,因此
checkbox
的id
应该被删除,或者必须以动态值(例如car id)作为后缀或前缀。
First, you should use class instead id for elements that will be present more than once. 首先,对于要多次出现的元素,应该使用class而不是id。 I suggest change
#SelectIndividual
for .SelectIndividual
on the checkbox input). 我建议改变
#SelectIndividual
为.SelectIndividual
的复选框输入)。 Another thing you should change is the carId
attribute, because is not semantic valid. 您应该更改的另一件事是
carId
属性,因为它在语义上无效。 You should use custom data attributes instead. 您应该改用自定义数据属性 。 This is how your code should look like:
这是您的代码应如下所示:
@foreach (var car in Model.Cars)
{
<tr data-carID="@Html.DisplayFor(modelItem => car.CarID)">
<td class="table-data">
<input id="SelectIndividual" name="Select" type="checkbox" />
</td>
$('#GetSelectedCars').click(function (event) {
var cars= new Array();
$('SelectIndividual:checked').each(function () {
var car= new Object();
car.ID = $(this).parent().parent().data('carID');
cars.push(car);
});
});
//keep doing things
Can you assign a class to all checkboxes in first column and then try this 您可以为第一列中的所有复选框分配一个类,然后再尝试
$('.cbClass:checked').each(function () {
tr = $(this).closest('tr');
// use the row
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.