このスレッドは、前号に続く 2 番目の問題です。クリック イベントを show-chart 関数にバインドした後も、正しいグラフ データを取得するために使用される従業員 ID の値を知る必要があります。
従業員 ID を showChartByEmpId(i) に渡すことは可能ですか?
HTML
<table id="empTable" class="tablesorter">
<thead>
<tr>
<th> Employee ID </th>
<th> Name </th>
<th> Department </th>
<th> Title </th>
<th> Show Chart </th>
</tr>
</thead>
<tbody>
<tr>
<td> 101 </td>
<td> John </td>
<td> R&D </td>
<td> Engineer </td>
<td> <button class="doPieChart"> Pie Chart </button> </td>
</tr>
<tr>
<td> 102 </td>
<td> David </td>
<td> R&D </td>
<td> Engineer </td>
<td> <button class="doPieChart"> Pie Chart </button> </td>
</tr>
</tbody>
</table>
<div class="secondary"> </div>
JS
function drawPieChart(i) {
$('.secondary').html('<div id="chart_div" class="div'+i+'">my graph '+(i+1)+' in fancybox</div>');
}
function showChart(i) {
drawPieChart(i);
}
function showChartByEmpId(i) {
showChart(i);
}
jQuery(function ($) {
//How to get emp Id here?
$(".doPieChart").each(function (i) {
$(this).on("click", function () {
showChartByEmpId(i);
$.fancybox("#chart_div");
}); // on click
}); // each
});
これがJSFIDDLEの私の例です