2

ajax経由でデータを送信するボタンがあります。次に、別のボタンに変更すると、2 つのボタンの間にサイクルが作成されます。

ただし、2 番目のボタンはクリックできません。理由はありますか?私のコードは次のとおりです。

<div class="<?PHP echo $value['id'];?>"><button class="checkin" id="<?PHP echo $value['id'];?>">Checkin</button></div>

<script type="text/javascript">
$(function() { // wrap inside the jquery ready() function


//Attach an onclick handler to each of your buttons that are meant to "approve"
$(".checkin").click(function(){

   //Get the ID of the button that was clicked on
   var id_of_item_to_approve = $(this).attr("id");


   $.ajax({
      url: "checkin_user.php", //This is the page where you will handle your SQL insert
      type: "POST",
      data: "eventid=<?PHP echo $eventId;?>" + "&id=" + id_of_item_to_approve, //The data your sending to some-page.php
      success: function(){
          alert("AJAX request was successfull");
          $("." + id_of_item_to_approve).html('<button class="checkout" id="' + id_of_item_to_approve + '">Check Out</button>');
      },
      error:function(){
          alert("AJAX request was a failure");
      }   
    });

});

//Attach an onclick handler to each of your buttons that are meant to "approve"
$(".checkout").click(function(){

   //Get the ID of the button that was clicked on
   var id_of_item_to_approve = $(this).attr("id");


   $.ajax({
      url: "checkin_user.php", //This is the page where you will handle your SQL insert
      type: "POST",
      data: "checkout=1&eventid=<?PHP echo $eventId;?>" + "&id=" + id_of_item_to_approve, //The data your sending to some-page.php
      success: function(){
          alert("AJAX request was successfull");
          $("." + id_of_item_to_approve).html('<button class="checkin" id="'+ id_of_item_to_approve +'">Check In</button>');
      },
      error:function(){
          alert("AJAX request was a failure");
      }   
    });

});

});
</script>
4

2 に答える 2

9

ボタンは ajax 応答から動的に生成されるため、次のように.on()を使用する必要があります。

$(document).on('click', '.checkout', function() {
    //rest of your code here
});
于 2013-11-07T03:28:24.323 に答える
1

次の 2 つのことを見逃しているためです。

  1. ドキュメントを監視するためのクリック イベントで有効にします。
  2. を使用して既存のボタン.checkout内に追加する新しいボタン。これを交換する必要があります。.checkinhtml()

ここで私は説明しています:

$(document).on('click', '.checkin', function(e) {
  e.preventDefault();
  // Ajax block
  $("#" + id_of_item_to_approve).replace('<button class="checkout" id="' + id_of_item_to_approve + '">Check Out</button>');
  // End Ajax block
});

$(document).on('click', '.checkout', function(e) {
  ...
});
于 2013-11-07T03:43:09.337 に答える