Jquery 如何从html表格列中获取值<;td>;选定行的名称(<;tr>;使用javascript
我有一个html表,它显示在我的web应用程序上,但我想 通过单击一行来选择它,然后有一个按钮来获取每个列的数据,作为我可以在javascript中使用的变量。这是我一直在尝试的,但它并不完整,因为我不知道该怎么做。记住我想要列中的内容。我的功能Jquery 如何从html表格列中获取值<;td>;选定行的名称(<;tr>;使用javascript,jquery,html,Jquery,Html,我有一个html表,它显示在我的web应用程序上,但我想 通过单击一行来选择它,然后有一个按钮来获取每个列的数据,作为我可以在javascript中使用的变量。这是我一直在尝试的,但它并不完整,因为我不知道该怎么做。记住我想要列中的内容。我的功能 function selectedRows() { var selectedItems = ('#ScannedLabelTabl; // i wanted to get the selected item (row) $.each
function selectedRows()
{
var selectedItems = ('#ScannedLabelTabl; // i wanted to get the selected item (row)
$.each(selectedItems, function (i, item) {
});
}
$('tr')。单击(函数(){
$('tr').removeClass('selected');
$(this.addClass('selected');
var td=$(this.children('td');
对于(变量i=0;i
在此处运行演示:我能想到的最简单的方法是在用户单击元素时向行/列添加一个类。CSS样式可以应用,jQuery的类选择器可以用于枚举所选元素 我将构建一个JSFIDLE
啊。太慢了。Ovilia的道具。假设您希望允许多个选择(不确定这是否是您想要的),您可以向执行此操作的单元格中添加单击事件处理程序
$("tr").click(eventHandler);
function eventHandler(){
//what this does is verifies of the element has the class, if it doesn't have the class is addeds, if it has the class it's removed
$(this).toggleClass("selected");
}
然后,要获取所有值,请执行以下操作:
function getAllValues(){
var arrayOfValues = [];
$(".selected").each(function(){
var value = $(this).//what you want to get val or text
arrayOfValues.push()
});
return arrayOfValues;
}
试试下面的代码
$(function () {
$("td").click(function () {
$("td", $(this).parent()).each(function () {
alert($(this).html());
})
})
你如何处理在桌子上的点击?每个表单元格的单击处理程序?如果是,您可以在其行中找到已单击单元格的索引。表是动态的还是静态的?如果是静态的,则可能是您可以在JavaScript中将数据创建为对象,然后使用该对象来编写表,但也可以更方便地引用事件处理程序。。。
$(function () {
$("td").click(function () {
$("td", $(this).parent()).each(function () {
alert($(this).html());
})
})
var tableRows = $("#ScannedLabelTabl").find("tr");//fetching all rows of the tblebody
$.each(tableRows, function( index, value ) {//iterating all fetched rows
var tdValue=$(value).find("td:nth-child(3)").html();
(value).find("td:nth-child(3)").addClass("adding");//get particular column