2017-02-21 83 views
0

我有以下的html:獲取表頭單元格內容

<div> 
    <table class="month"> 
     <tbody> 
      <tr> 
       <th class="month">February 2017</th> 
      </tr> 
     ... 

我想獲得「2017年二月」使用jQuery,所以我可以用Ajax發送到一個視圖。我已經嘗試了一些東西,無法讓這個工作。這是我的時刻了:

$('#next').click(function() { 
    $.ajax({ 
     url: my_url, 
     method: 'GET', 
     data: { 
      current: $('#month').find("th.month").html(), // data you need to pass to your function 
      other: "other" 
     }, 
     success : function (json) { 
      console.log(json);   
     } 
    }); 
}); 

它,當我點擊了下一個ID的鏈接觸發。其他數據發送很好。當前返回無。我也嘗試過使用val()。

得到divs/table/th等約定真的讓我困惑。

感謝

回答

3

#操作符根據ID找到,而不是類(即。)。

從HTML,所有你需要做的,找到 「2017年2月」 就是用:

$("th.month").html() 

JS小提琴: https://jsfiddle.net/fy3bdyLb/

+0

這是解決它,謝謝。知道這很有用。是我不知道區別的課程。 –

2

試試這個

<th class="month" id="month">February 2017</th> 

你叫你的Ajax 「#month」 但你有 「月」 作爲您的TH類。它應該是你的ajax數據中的「.month」。

ID爲「#」

「。」對於類

0

您與$('#month')選擇<table class="month">,它應該是$('.month')因爲它是一個類而不是一個id。 雖然我不確定這是唯一的問題。