Jquery,ajax 按钮不起作用
Jquery, ajax button not working
我有一个通过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
});
因为你错过了两件事:
- 为要监视的文档按钮启用单击时事件。单击复选*。
- 您追加在内部的新
.checkout
按钮使用html()
退出.checkin
按钮。这应该被替换。
在这里我描述的是:
$(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) {
...
});
相关文章:
- 点击按钮输入不起作用
- Js.erb VS按钮标记-不'不起作用.为什么?
- 按钮字段确认点击不'不起作用
- JQuery示例不起作用-“;对象没有't支持属性或方法'按钮'&”;
- ng disabled在chrome中不起作用.按钮在角度上未禁用
- JS动态添加字段-删除按钮不起作用
- 角度无线电按钮ng模型不起作用
- 当我通过.aspx.cs创建按钮时,它不起作用
- 一键启动按钮不起作用
- 带有向下钻取的 Amchart 图表工作正常,但“后退”按钮不起作用
- 为什么不'这个HTML按钮不起作用
- 使用javascript后,提交按钮不起作用
- 表单中的提交按钮在 PHP 打印中不起作用
- 镀铬延长件内部的聚合物纸按钮不起作用
- 我的提交按钮不起作用
- 搜索按钮谷歌地图在我的vf页面上不起作用
- AngularJs:多次点击同一个按钮不起作用
- 我尝试将引导下拉按钮添加到 但下拉菜单不起作用.我该怎么做
- visualforce页面javascript按钮点击不起作用
- 单击功能不起作用按钮