">Checkin $(func-6ren">
gpt4 book ai didi

javascript - Jquery,ajax 按钮不工作

转载 作者:行者123 更新时间:2023-11-28 04:35:54 26 4
gpt4 key购买 nike

我有一个通过 ajax 提交数据的按钮。然后,当我将其更改为不同的按钮时,这会在两个按钮之间创建一个循环。

但是第二个按钮不可点击。知道为什么吗?这是我的代码:

<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>

最佳答案

由于您的按钮是从 ajax 响应动态生成的,因此您需要使用 .on() ,比如:

$(document).on('click', '.checkout', function() {
//rest of your code here
});

关于javascript - Jquery,ajax 按钮不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19827458/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com