简体   繁体   English

从选定的TD中获取价值

[英]Getting value out of selected td

Currently i have a selectable table which i can drag and select tds in a column. 目前,我有一个可选择的表格,可以在其中拖动并选择tds。 However i would like to get all the values from each of this td cells. 但是我想从每个td单元中获取所有值。 How do i go about doing this? 我该怎么做? i have tried .each() but it still does not work 我已经尝试过.each(),但仍然无法正常工作

<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>");
    }
%>

You can iterate over all the td's in your document and check if has the selected class.. 您可以遍历文档中的所有td并检查是否具有选定的类。

IF yes then get that Try this 如果是的话,那就试试看

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

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

You can use element.innerText to get the text content of any HTML element. 您可以使用element.innerText获取任何HTML元素的文本内容。 For example, on this page you can use $(".question-hyperlink")[0].innerText to get the question text. 例如,在此页面上,您可以使用$(".question-hyperlink")[0].innerText获取问题文本。 $(selector)[0] is a plain old HTML element. $(selector)[0]是一个普通的旧HTML元素。

Or, staying in jQuery, you can use $(selector).text() to do the same thing. 或者,使用jQuery,您可以使用$(selector).text()进行相同的操作。 Something like $(".question-hyperlink").text() works exactly the same way. 类似于$(".question-hyperlink").text()工作方式完全相同。

get TD of each TR 得到每个TR的TD

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

});

The selectable plugin adds the class ui-selected to any selected element, so simply: selectable插件将ui-selected类添加到任何选定的元素中,因此很简单:

$('.ui-selected')

will give you all selected elements. 将为您提供所有选定的元素。 Limit to your .tdItem s if you have other selectable things by using: 如果您还有其他可选内容,请使用.tdItem限制:

$('.tdItem.ui-selected')

If you want to do something every time the selection changes, then you can use the selected or stop callback in the .selectable({...}) options . 如果您希望每次选择更改时都要做某事,则可以.selectable({...})选项中使用selectedstop回调 Putting it all together gives us something like: 将所有内容放在一起,我们得到的像是:

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 . 在此处查看jsfiddle演示

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

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