jqueryでクリックすると親IDを取得しようとしています。子のクリックで親のIDを取得する
次のように私のDjangoテンプレートは次のように
<table id="archive-table" class="table table-hover table-vcenter">
<thead>
<tr>
<th>Make and model</th>
<th>First registration</th>
</tr>
</thead>
<tbody>
{% for calculation in calculations %}
<tr data-archive-row class="archive-row" data-calculation-id={{ calculation.id }}>
<td>{{ calculation.first_registration }}</td>
<td>{{ calculation.body }}</td>
</tr>
{% endfor %}
</tbody>
</table>
そして、私のjsがある:
<script>
$(document).ready(function() {
$('#archive-table').on('click', '[data-archive-row]', function (e) {
var calculation_id = e.target.dataset['calculationId'];
alert(calculation_id)
})
});
</script>
私は関係なく、私は子供をクリックした場合、親のdataset['calculationId']
を取得することはできませんどのように。
私のコードでは、が未定義になっています。しかし、たとえば私がdata-calculation-id={{ calculation.id }}
を1つに追加するのであれば、tdとし、それをクリックすれば正しいidが得られます。
子供が親をクリックしても、親を親から取得する方法はありますか?
これは良いです(私はたぶん 'this.dataset'を使用します)が、' e.target'と 'e.currentTarget'の違いを理解することが重要です。 – dfsq
そうですね、純粋なjsとjQueryを混ぜないほうが良いと思います。 –