2013-10-30 35 views
0

嗨我已經做了很多搜索到處都在谷歌和該網站,但我絕對有問題與我的選擇。找到它的類最近的跨度到tr

嗨有一個由php生成的動態表。我有一些編輯內容的按鈕,所以我嘗試編輯它,同時點擊編輯按鈕,但似乎沒有找到好的項目。事實上在什麼都找不到。

Acutaly我:

名爲編輯每行一個按鈕,它有一個名爲changer_etat

然後,我有jQuery代碼類:

$('.changer_etat').click(function(){ 
    var date_depart = $(this).closest('tr').find('span.date_depart').val(); 
    console.log(date_depart); 
}); 

我試圖找到一個spandate_etat在當前tr其中是我點擊的按鈕。

但是ac tualyit沒有回報我,我已經嘗試了很多東西,並做了一些搜索,但沒有成功。

我已經試過這

$('.changer_etat').click(function(){ var date_depart = $(this).closest('span.date_depart').val(); console.log(date_depart); });

返回的幫助未定義

Anykind將非常感激。

回答

2

使用需要.text().html().val()

$('.changer_etat').click(function(){ 
    var date_depart = $(this).closest('tr').find('span.date_depart').text(); 
    console.log(date_depart); 
}); 
+1

感謝它很好。你是一個天才,我從一週開始就開始使用Jquery,這並不容易......我以爲.val就是要找到每一個價值。非常感謝 – user2506760

+0

@ user2506760'.val()'與'input,textarea,select etc'等表單元素一起工作。歡迎來到幫助:) –

1

您可能需要使用動態選擇器,因爲您的表正在動態生成。另外,正如其他人已經注意到,您將要使用html,因爲您正在抓取跨度的html。跨度不具有值屬性。

$('html').on('click', '.changer_etat', function(){ 
    var date_depart = $(this).closest('tr').find('span.date_depart').html(); 
    console.log(date_depart); 
});  
1

使用html()

$('.changer_etat').click(function(){ 
    var date_depart = $(this).closest('tr').find('span.date_depart').html(); 
console.log(date_depart); 
}); 
0

有之間的錯字 「找到( 'span.date_depart')」 和「找到一個跨度叫做date_etat「?至少這對Firefox 25.0/Linux有效:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Test</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('.changer_etat').click(function() { 
       var date_depart = $(this).closest('tr').find('span.date_etat').html(); 
       console.log("date_depart="+date_depart); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <table> 
     <tbody> 
      <tr><td><span class="date_etat">i am content 1</span></td><td><button class="changer_etat">button action</button></td></tr> 
      <tr><td><span class="date_etat">i am content 2</span><button class="changer_etat">button action</button></td></tr> 
     </tbody> 
    </table> 
</body> 
</html> 
相關問題