問題は分かりやすいと思いますが、ここでは何が起きているのでしょうか。divに新しい要素を追加すると、jQueryは新しい要素の作業を停止します
<div class="post-basic" id="post-{unique id}">
<div class="title-card">
<div class="avatar-sec">
<img src="{link to avatar}" class="avatar" ref="Avatar" />
</div>
<div class="name-card">
<span class="display-name">{display name}</span>
<span class="handle">@{handle}</span>
</div>
<div class="action">
<div class="follow-button {filled with either: follow or followed}" id="{unique id}">
{filled with either: follow or followed}
</div>
</div>
</div>
</div>
とあなたはdiv.followボタンをクリックしたとき、それはに変更しますように私はjQueryが設定されている:私はこのように設定されているページ全体で繰り返される「ポスト」要素を持っています'フォローして' PHPページにPOSTを送信します。そのように設定します。
$('.follow-button').click(
function() {
var user_id = $(this).attr('id');
var follow_url = "./func/follow.func.php";
if ($(this).hasClass("follow")) {
$(this).removeClass('follow').addClass('followed').text('followed');
$.ajax({
type: "POST",
url: follow_url,
data: {id:user_id}, // serializes the form's elements.
success: function(data)
{
$('#'+user_id).parents('.post-basic').remove();
$.ajax({
type: "POST",
url: "./func/gather.basic.func.php",
data: {}, // serializes the form's elements.
success: function(data)
{
var response = $.parseJSON(data);
$('.posts').append('<div class="post-basic" id="post-'+ response[0].streamID_gather +'"><div class="title-card"><div class="avatar-sec"><img src="'+ response[0].avatarSrc +'" class="avatar" ref="Avatar"/></div><div class="name-card"><span class="display-name">'+ response[0].display_name +'</span><span class="handle">@'+ response[0].name +' '+ response[0].followsYou +'</span></div><div class="action"><div class="follow-button '+ response[0].follow +'"id="'+ response[0].streamID_gather +'">'+ response[0].follow +'</div></div></div></div>');
}
});
}
});
}
}
);
私はちょうどので、私はこれと同じことを行うが、本質的に他の方法の多くがあります確信しているjQueryを使って始めるのです:div.follow-button
がクリックされた場合、続くへdiv
変更内のテキストをclass
はfollowed
に変更されています。次に、POST
リクエストを"./func/follow.func.php";
に送信します。要求が正常に処理されると、"./func/gather.func.basic.php";
に別の要求が送信され、json
配列が返されます。配列を使用すると、jQuery
はコンテナdiv.posts
に新しいpost
という応答データを追加します。それは何か。上記のすべてがうまくいっています。しかし、私がしようとすると、新しいdiv.post
とフォローボタンをクリックしても動作しません。 jQueryが既にロードされているか、それとも何ですか?
あなたはajax呼び出しの完了時に何かしたいと思われます - 別のajax呼び出しを入れ子にするのはここでは "奇妙"です。おそらく約束を使用するか? https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323 –
イベントをすべてが入っているコンテナにフックしたい場合があります。 –
どこに最初の 'follow'ボタンをクリックした後、新しいfollowボタンのcreateに' onclick'をバインドしますか? –