2012-09-25 115 views
0

目前我有一个可选择的表格,我可以在列中拖动和选择tds。不过,我想从每个td单元中获取所有值。我如何去做这件事?我曾尝试。每(),但它仍然无法正常工作从选定的td中获取价值

<style type="text/css"> 
.ui-selecting { background: #FECA40; } 
.ui-selected { background: #F39814; } 
</style> 

<script> 
    $(function() { 
    $("table").selectable({ 
     filter: ".tdItem" 
    }); 
}); 
</script> 
</head>  

<body> 


<table width="100%" border="1"> 
<% 
    for(String t: time){ 
     out.println("<tr>"); 
     int i=0; 
     for(String room: rooms){ 
      out.println("<td class='tdItem' align='center'>"); 
      out.println(room+" "+t); 
      out.println("</td>"); 
      i++; 
     } 
     out.println("</tr>"); 
    } 
%> 

+0

获取值/何时? '$( 'td.tdItem')'? –

+0

我应该如何遍历我选择的tds? – smallcat31

回答

0

可以遍历所有TD的文档中,并检查是否有选择的类..

YES时拿到 试试这个

var $td = $('tr td'); 

$.each($td , function(i){ 
    if($(this).hasClass('tdItem'){ 
     console.log($(this).text()) 
    } 
}); 
​ 
+0

但我所有的tds都有相同的类别。这将给我所有的tds信息,而不是选定的1s – smallcat31

+0

但是,如果你的td被选中,那么它将被赋予一个不同的类权利。通过检查元素 –

+0

找出这个类是什么。为什么不只是'$('td .tdItem')'?当jQuery能够为你做滤波时,你自己循环遍历所有tds没有意义。 –

0

您可以使用element.innerText得到任何HTML元素的文本内容。例如,在此页面上,您可以使用$(".question-hyperlink")[0].innerText获取问题文本。 $(selector)[0]是一个普通的旧HTML元素。或者,留在jQuery中,你可以使用$(selector).text()来做同样的事情。类似$(".question-hyperlink").text()的工作方式完全相同。每个TR

$('table tr').each(function(i){ 
    $('td',this).each(function(){ 
      console.log($(this).text()) 
    }); 

}); 
0

GET TD添加类ui-selected到任何选定的元素,所以干脆:

$('.ui-selected') 

会给你所有选中的元素。如果你想每次选择更改,那么你可以使用the selected or stop callback in the .selectable({...}) options时间做某事

$('.tdItem.ui-selected') 

:限制你.tdItem■如果您有其他选择的东西使用。综合起来就是这样:

function afterSelectionChanged(event, ui){  
    var $selectedElements = $('.tdItem.ui-selected'); 
    // Do something with the selected elements 
} 

$("table").selectable({ 
    filter:'.tdItem', 
    stop: afterSelectionChanged 
}); 

See jsfiddle demo here

0

可选择的插件