やりたいことは、表の1行を一つのボタンにしたいと考えています。以下の方法を自分なりに考えましたが、全く別の方法がありましたら、教えてください。よろしくお願いいたします。
試したこととしては、
一つ目
hrefの値を取ってきて、そのurlにwindow.location.href
で遷移するというやり方です。参考サイト
しかし、hrefの値がdjangoのテンプレートタグのurlタグなのでうまく遷移しません。
二つ目は下のjsコードに書いたようなtrigger('click')を使うやり方です。
しかし、aタグはクリックできないようです。
どうしたら良いでしょうか。
html
1コード 2 <tr class="book_title_tr"> 3 <th scope="row"> 4 <a href="{% url 'book_title' bookT_page_Flag=1 bookID=book.bookID %}">{{book.bookTitle</a> 5 </th> 6 <td>{{ book.bookDate }}</td> 7 </tr> 8
js
1コード 2$('.book_title_tr').children('th').children('a').trigger('click');
追記
すみません。上の書き方だと良くなかったです。もう一度下に書き直しました。
もう全体を以下に示します。
console.logがたくさん出力されてしまいます。
html
1コード 2<table class="table book_table"> 3 4 <thead> 5 <tr> 6 <th scope="col">本のタイトル</th> 7 <th scope="col">発行日</th> 8 </tr> 9 </thead> 10 <tbody> 11 <!-- 本の一覧を表示 --> 12 {% for book in bookT %} 13 <tr class="book_title_tr"> 14 <th scope="row"><a href="{% url 'book_title' bookT_page_Flag=1 bookID=book.bookID %}">{{book.bookTitle}}</a></th> 15 <td>{{ book.bookDate }}</td> 16 </tr> 17 {% endfor %} 18 </tbody> 19</table> 20 21 22<script> 23//-------------book_title.html--------- 24$(function () { 25$('.book_title_tr').on('click', function(){ 26 var i = $('.book_title_tr').index(this); 27 console.log($('.book_title_tr').eq(i).children('th').children('a')); 28 $('.book_title_tr').find('th a').eq(i).click(); 29 30}); 31}); 32</script>