2016-04-07 15 views
0

私は、webgrid経由でレンダリングされるHTMLテーブル内のテキストを取得したいと考えています。私が望むテキストはdivのクラスproductIDの中にあります。私の開始基準点は同じ行にありますが、最後のtdはクラスspan2です。私はjQueryのclosest()メソッドを使用しようとしていますが、返される値はありません。jQueryを使用して、最も近いスパンからテキストを取得します。

レンダリングされたHTMLの部分と私のjQueryの機能は以下を参照してください:

HTML:

<tr> 
    <td class="span1"><div class="productID">1</div></td> 
    <td class="span2">Listing</td> 
    <td class="span2">Full Districtution</td> 
    <td class="span2">$1,350.00</td> 
    <td class="span2">2016-01-01</td> 
    <td class="span2"><div title="This is my brand new title!" data-original-title="" class="priceToolTip">2016-04-30</div></td> 
    <td><a href="/product/AddOrEditProduct?productID=1">Select</a></td> 
</tr> 

のjQuery:

$(".priceToolTip").mouseover(function() { 
    var row = $(this).closest("span1").find(".productID").parent().find(".productID").text(); 
    console.log("Closest row is: " + row); 
}); 

答えて

1

.span1.priceToolTipの最も近い要素ではありません。以下のようにclosest("tr").find(".span1 .productID")を使用してください。

2

.closest()メソッドは、祖先で一致するものを探します。

var productID = $(this).closest('tr').find('.productID').text(); 

または::

var productID = $(this).parent().find('.productID').text(); 

または:

var productID = $(this).siblings('.span1').find('.productID').text(); 
だから trは、そのようによう .productIDを探しつかむためにそれを使用することができます
関連する問題