利用AJAX动态生成的标签如何添加监听事件
<script>
$(function() {
$.ajax({
url: "/xiaomi/shopcart/chk",
method: "GET",
success: function(response) {
console.log(response);
// 获取所有的商品
let $cartList = response.objectMap.cartList;
console.log($cartList);
for(let i = 0; i < $cartList.length; i++) {
console.log("....." + $cartList[i]);
let $list_item = $("<div>").addClass("list-item");
let $col_check = $("<div>").addClass("col-check");
let $checkbox = $("<input>").attr("type", "checkbox");
$col_check.append($checkbox);
let $col_img = $("<div>").addClass("col-img");
let $img = $("<img>").attr("src", $cartList[i].goodsImages[0].path);
$col_img.append($img);
let $col_name = $("<div>").addClass("col-name").text("商品名称");
let $col_price = $("<div>").addClass("col-price").text("商品单价");
let $col_num = $("<div>").addClass("col-num");
let $add = $("<a>").attr("href", "#").append($("<em>").addClass("glyphicon glyphicon-minus"));
let $col_input = $("<input>").attr("type", "text").val($cartList[i].buyCount);
let $sub = $("<a>").attr("href", "#").append($("<em>").addClass("glyphicon glyphicon-plus"));
$col_num.append($add).append($col_input).append($sub);
let $col_total = $("<div>").addClass("col-total").text($cartList[i].subtotal);
let $operation = $("<div>").addClass("col-action").append($("<button>").addClass("glyphicon glyphicon-remove"));
$col_total.append($operation);
let $clear = $("<div>").css("clear", "both");
$list_item.append($col_check).append($col_img).append($col_name).append($col_price).append($col_num)
.append($col_total).append($operation).append($clear);
$("#shopcart-box").append($list_item);
}
},
error: function() {
console.log("请求走丢了,小二正在路上...");
}
});
$("#shopcart-box").on("click",".list-item .col-action .glyphicon-remove",function (){
alert("1");
})
});
</script>