繁体   English   中英

第二次单击无法使用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");
                            //}
                        }
                    });
                });
            });

谁能指导我我错了。 我会的。谢谢

在每个内部单击。 使用$('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");
                 //}
               }
      });
});

认为事件委派有问题,

更改

$(this).click("click",  function(){

$(document).on("click", ".removeItem", function(){

使用点击功能和事件来防止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");
                            //}
                        }
                    });
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM