以下のようなScriptを書いており、ボタンを押下すると、id="Apply"を"Applyed"に変更する処理を書いています。
$("#Apply").clickの処理は正常に行われ、画面上もID="Applyed"に変化するのですが、その後、idがApplyedになっているボタンを押下しても$("#Apply").clickのAjaxが起動してしまいます。
どのようにすれば、$("#Applyed").clickを起動させる事ができるのか不明の為、ご教授いただけますと幸いです。
HTML
1<script> 2$(document).ready(function () { 3 function getCookie(name) { 4 var cookieValue = null; 5 if (document.cookie && document.cookie != '') { 6 var cookies = document.cookie.split(';'); 7 for (var i = 0; i < cookies.length; i++) { 8 var cookie = jQuery.trim(cookies[i]); 9 // Does this cookie string begin with the name we want? 10 if (cookie.substring(0, name.length + 1) == (name + '=')) { 11 cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); 12 break; 13 } 14 } 15 } 16 return cookieValue; 17 } 18 var csrftoken = getCookie('csrftoken'); 19 function csrfSafeMethod(method) { 20 // these HTTP methods do not require CSRF protection 21 return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); 22 } 23 $.ajaxSetup({ 24 crossDomain: false, // obviates need for sameOrigin test 25 beforeSend: function (xhr, settings) { 26 if (!csrfSafeMethod(settings.type)) { 27 xhr.setRequestHeader("X-CSRFToken", csrftoken); 28 } 29 } 30 }); 31}); 32//ここまでおまじないと思って記述 33//以下Ajax 34$("#Apply").click(function (event) { 35 event.preventDefault(); 36 var ine = $(this); 37 $.ajax({ 38 url: ine.attr('data-href'), 39 method: 'POST', 40 timeout: 10000, 41 dataType: "json", 42 }) 43 .done(function (data) { 44 //ここでIDを変更している 45 $('#Apply').attr("id","Applyed"); 46 var link = ine.attr('data-href').replace('apply_ajax/','apply_ajax/kaijo'); 47 $('#Applyed').attr("data-href",link); 48 $('#Applyed').text("応募済"); 49 var text = ', <a href="/user/'+data.userid+'">'+data.username+'</a>' 50 $('#applyhoge').append(text); 51 }) 52 .fail(function (data) { 53 alert("fail"); 54 }) 55}); 56//以下Ajax 57$("#Applyed").click(function (event) { 58 event.preventDefault(); 59 var ine = $(this); 60 $.ajax({ 61 url: ine.attr('data-href'), 62 method: 'POST', 63 timeout: 10000, 64 dataType: "json", 65 }) 66 .done(function (d) { 67 $('#Applyed').attr("id","Apply") 68 $('#Applyed').attr("data-href","hogehoge") 69 }) 70 .fail(function (data) { 71 alert("fail"); 72 }) 73}); 74</script>
html(ボタン部分)
1<a id="Apply" class="btn btn-primary" data-href="{% url 'apply' pk=list.0.pk time_id=i.0.id %}">応募済</a>aaa
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/12 02:52