while ループを使用して作成されたリストがあり、ユーザーが削除ボタンをクリックすると、ajax を使用してデータベース内のエントリが削除され、リストが再描画されます。ユーザーが別のアイテムを削除しようとするまで、これはすべて正常に機能します。ボタンは何もせず、エラーも発生しません。それを機能させる唯一の方法は、ページを更新することです。
私は ajax を変更したので、返されるのはオリジナルと同じクラスのボタンだけで、少なくとも他に何もない場合にアラートをトリガーできるかどうかを確認できます。
AJAX
<script type="text/javascript">
$(document).ready(function() {
$('.trash_btn').click(function(){
alert('bob');
var question = $(this).next('.id_question').val();
var user = '<? echo $id; ?>';
alert(question+' '+user);
$.msgbox("Are you sure you want to delete the selected question?", {
type: "error",
buttons: [
{type: "submit", value: "Yes"},
{type: "cancel", value: "No"}
]
}, function(result) {
if (result == 'Yes') {
$.ajax({
url: 'ajax/opinion_del.php',
type:'POST',
data: 'question='+ question +'&user='+user,
success: function(response){
$('#responses_table').html(response);
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
} // End of success function of ajax form
});
}
});
});
});
</script>
表の関連部分
<td class="details">
<input type="button" name="del_question" value="" class="trash_btn">
<input type="hidden" class="id_question" name="question_id" value="<? echo $row['questionID']; ?>">
<input type="hidden" class="id_user" name="user" value="<? echo $id; ?>" >
</td>
動作しないテスト ボタンを作成する AJAX 出力 (元々は JSON を使用していましたが、テスト用に削除されました)
$smeg = '<input type="button" value="test" class="trash_btn">';
echo $smeg;