使用 jquery 第二次点击无效
second time click does not working using jquery
我有通过 php 代码动态生成的列表。我想通过单击删除行。我的代码第一次工作正常,第二次工作正常
HTML
<li>
<div class="pers-left-container">
<img src="<?php echo $cProduct['thumb']; ?>" />
</div>
<div class="pers-right-container">
<span class="sidebar-product-name"><?php echo $cProduct['name']; ?></span>
<a href="javascript:void(0);" class="close-container-product removeItem">X
<form id="removeItemForm" method="post" >
<input type="hidden" value="<?php echo $cProduct['product_id']; ?>" name="removeProductId" class="removePID" />
</form>
</a><br />
<label>Qty: </label><input type="number" value="1" min="0" max="100" step="5" /><br />
<span class="price-container-product">Amt: <?php echo $cProduct['price']; ?></span>
</div>
<div class="clearfix"></div>
</li>
JS
$( ".removeItem" ).each(function(index) {
$(this).click("click", function(){
//var otherInput = $(this).find('.pID').val();
removePID = {'removeProductId':$(this).find('.removePID').val()}
alert(removePID['removeProductId']);
$.ajax({
type:"post",
url:"index.php?route=common/personaliseyourflowers",
data:removePID,
success:function(response){
//alert(response);
if(response) {
$(".reloadCOnt").load(window.location + " .reloadCOnt");
}
//} else {
//$(".reloadCOnt").load(window.location + " .reloadCOnt");
//}
}
});
});
});
任何人都可以指导我哪里错了。我会感谢
认为事件委托有问题,
改变
$(this).click("click", function(){
至
$(document).on("click", ".removeItem", function(){
.click inside.each?!!使用 $('body').on('click' , selector , function(){});
$( 'body' ).on("click",".removeItem", function(){
//var otherInput = $(this).find('.pID').val();
removePID = {'removeProductId':$(this).find('.removePID').val()}
alert(removePID['removeProductId']);
$.ajax({
type:"post",
url:"index.php?route=common/personaliseyourflowers",
data:removePID,
success:function(response){
//alert(response);
if(response) {
$(".reloadCOnt").load(window.location + " .reloadCOnt");
}
//} else {
//$(".reloadCOnt").load(window.location + " .reloadCOnt");
//}
}
});
});
使用 on click 函数和事件来防止 href :
$( ".removeItem" ).click(function(e) {
e.preventDefault(); // to stop redirecting to href
//var otherInput = $(this).find('.pID').val();
removePID = {'removeProductId':$(this).find('.removePID').val()}
alert(removePID['removeProductId']);
$.ajax({
type:"post",
url:"index.php?route=common/personaliseyourflowers",
data:removePID,
success:function(response){
//alert(response);
if(response) {
$(".reloadCOnt").load(window.location + " .reloadCOnt");
}
//} else {
//$(".reloadCOnt").load(window.location + " .reloadCOnt");
//}
}
});
});
我有通过 php 代码动态生成的列表。我想通过单击删除行。我的代码第一次工作正常,第二次工作正常
HTML
<li>
<div class="pers-left-container">
<img src="<?php echo $cProduct['thumb']; ?>" />
</div>
<div class="pers-right-container">
<span class="sidebar-product-name"><?php echo $cProduct['name']; ?></span>
<a href="javascript:void(0);" class="close-container-product removeItem">X
<form id="removeItemForm" method="post" >
<input type="hidden" value="<?php echo $cProduct['product_id']; ?>" name="removeProductId" class="removePID" />
</form>
</a><br />
<label>Qty: </label><input type="number" value="1" min="0" max="100" step="5" /><br />
<span class="price-container-product">Amt: <?php echo $cProduct['price']; ?></span>
</div>
<div class="clearfix"></div>
</li>
JS
$( ".removeItem" ).each(function(index) {
$(this).click("click", function(){
//var otherInput = $(this).find('.pID').val();
removePID = {'removeProductId':$(this).find('.removePID').val()}
alert(removePID['removeProductId']);
$.ajax({
type:"post",
url:"index.php?route=common/personaliseyourflowers",
data:removePID,
success:function(response){
//alert(response);
if(response) {
$(".reloadCOnt").load(window.location + " .reloadCOnt");
}
//} else {
//$(".reloadCOnt").load(window.location + " .reloadCOnt");
//}
}
});
});
});
任何人都可以指导我哪里错了。我会感谢
认为事件委托有问题,
改变
$(this).click("click", function(){
至
$(document).on("click", ".removeItem", function(){
.click inside.each?!!使用 $('body').on('click' , selector , function(){});
$( 'body' ).on("click",".removeItem", function(){
//var otherInput = $(this).find('.pID').val();
removePID = {'removeProductId':$(this).find('.removePID').val()}
alert(removePID['removeProductId']);
$.ajax({
type:"post",
url:"index.php?route=common/personaliseyourflowers",
data:removePID,
success:function(response){
//alert(response);
if(response) {
$(".reloadCOnt").load(window.location + " .reloadCOnt");
}
//} else {
//$(".reloadCOnt").load(window.location + " .reloadCOnt");
//}
}
});
});
使用 on click 函数和事件来防止 href :
$( ".removeItem" ).click(function(e) {
e.preventDefault(); // to stop redirecting to href
//var otherInput = $(this).find('.pID').val();
removePID = {'removeProductId':$(this).find('.removePID').val()}
alert(removePID['removeProductId']);
$.ajax({
type:"post",
url:"index.php?route=common/personaliseyourflowers",
data:removePID,
success:function(response){
//alert(response);
if(response) {
$(".reloadCOnt").load(window.location + " .reloadCOnt");
}
//} else {
//$(".reloadCOnt").load(window.location + " .reloadCOnt");
//}
}
});
});