Php 为什么这行不通?如何做到这一点?

Php 为什么这行不通?如何做到这一点?,php,javascript,jquery,html,Php,Javascript,Jquery,Html,这是我的HTML和PHP代码 颜色盒很好,它也显示了按钮 //This is the Colorbox code <div style="display: none"> <div id="remove"> <h1>Are you sure you wish to remove this product</h1> <h2>Enter Captcha below to confirm</h2> //Just a

这是我的HTML和PHP代码

颜色盒很好,它也显示了按钮

//This is the Colorbox code
<div style="display: none">
<div id="remove">
    <h1>Are you sure you wish to remove this product</h1>
    <h2>Enter Captcha below to confirm</h2> //Just a Captcha-like behavior
    <input type="text" value="<?php echo $captcha_value; ?>" id="display" readonly ><br /> <br>
    <input type="text" name="captcha" value="" id="input" maxlength=6/> <br> <br>
</div>
</div>

//To Display all products
<?php foreach ($products as $product) { ?>
<td>
   <div>
     <img src="<?php echo $product['image']; ?>"/>
   </div>
   <div>
     <a onClick="loadColor(<?php echo $product['opv_id']; ?>)" class="remove-item">
       <img src="<?php echo $product['product_remove_image']; ?>"/>
     </a> //To know which product's link was clicked
   </div>
</td>
<?php } ?>

//loadColor function which will append a button to Colorbox
<script type="text/javascript">
function loadColor(opv_id) { //To create a button and append to the colorbox and call colorbox
  $('#remove-product').remove();
  string = '<button value="' + opv_id + '" id="remove-product">Submit</button>';
  $(string).appendTo('#remove');
  jQuery.colorbox({inline: true, href:"#remove", width:"50%"});
}
</script>

<script type="text/javascript">
$('#remove-product').click(function() {
  alert('Clicked');
  //Do something here!!
})
</script>
//这是颜色框代码
是否确实要删除此产品
在下面输入Captcha以确认//只是类似于Captcha的行为
)“class=”删除项目“>
"/>
//了解单击了哪个产品的链接
//loadColor函数,该函数将向Colorbox附加一个按钮
函数loadColor(opv_id){//创建一个按钮并附加到colorbox并调用colorbox
$(“#删除产品”).remove();
字符串='Submit';
$(字符串).appendTo('#remove');
colorbox({inline:true,href:#remove”,width:“50%”);
}
$(“#删除产品”)。单击(函数(){
警报(“点击”);
//在这里做点什么!!
})
明白了:)


那么,还有其他更简单的方法来实现我在上面尝试的操作吗?

您需要对动态添加的元素使用事件委派:

$('#remove').on("click", '#remove-product', function () {
    alert('Clicked');
    //Do something here!!
});

检查

查找“活动授权”。非常感谢您的可能副本。请看我编辑的问题。@Razor您已经用正确的方式完成了:)