2012-10-30 48 views
0

我有一张5 x 5的表格。每个单元格都是一个asp.net文本框。 我想用非空文本框获取最后一行的5个文本框值。 例如:无法通过jQuery获取html表格中的兄弟值

enter image description here

这应该让地狱啊,我FRIE ND

但我得到的所有值,但最后一个(ND)。我不知道为什么。这里是我的代码:

// RIGHT HERE, $(this) referes to the whole table 
$(this).find('input[value!=""]') 
     // I get the last edited textbox 
     .last() 
     // Go up to the parent <td> 
     .parent('td') 
     .siblings('td') 
     // I get all my brothers of type input 
     .children(':input') 
     .each(function() { 
      alert($(this).val()); 
     }); 

我做错了什么,但什么? 谢谢。

回答

1

问题所在当您拨打.siblings() - 返回全部其他<td>与您当前选择的<td>元素具有相同父元素的元素。这个问题可以通过遍历DOM中的另一个步骤来解决,然后像@Adrian,@Quintin Robinson,@Sushanth - 和@wirey所显示的那样选择孩子。

我想我会发布一个答案,所以你会有一个解释,因为所有其他答案解决了你的问题,但没有解释什么是错的。现在这里是我将如何改变你的代码:)

$(this).find('input[value!=""]:last') 
    .parents('tr') 
    .find('td input') 
    .each(function() { 
     alert($(this).val()); 
    }); 
+0

这个工程。谢谢 !! – anmarti

1

您应该使用parents()和搜索tr,然后将所有input里面:

$(this).find('input[value!=""]') 
     .last() 
     .parents('tr') 
     .find('input') 
     .each(function() { 
      alert($(this).val()); 
     }); 
1

你可以使用这样的事情..

$(this).find('input[value!=""]:last') 
     .parents("tr:first") 
     .find(":text") 
     .each(...); 
0

试试这个

$(this).find('input[value!=""]') 

     .closest('tr') 
     .prev('tr') 
     .children(':input') 
     .each(function() { 
      alert($(this).val()); 
     }); 
1

你应该这样做

$(this).find('tr').filter(function() { // find all trs 
    // returns the row if all inputs are filled 
    return $(this).find('input').filter(function() { 
     return $(this).val().length > 0 
    }).length == 5; // check to make sure row has 5 unempty textboxes 
}).last() // get the last tr returned with no empty textboxes 
.find('input').each(function() { // find inputs 
    console.log($(this).val()); 
});​ 

http://jsfiddle.net/E5hnA/

你的问题就出在你不按行过滤..但找到最后unempty文本框..然后开始遍历有

$(this).find('input[value!=""]') // find all inputs that are not empty 
    .last() // get the last one 

时,有一个输入会发生什么在最后一行有一个非空文本框?这就是你所得到的。我在我的例子中做的是。

找到所有行>过滤器,返回那些具有填充所有输入>获得的最后一个返回集合中>然后做什么 - 因为现在你有最后一行的所有输入充满