亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按鈕刪除不刪除產品

按鈕刪除不刪除產品

PHP
慕無忌1623718 2022-07-29 10:50:48
我嘗試在 javascript 消息中包含一個刪除按鈕。當我單擊垃圾箱時,會出現一條消息以確認操作。但是如果我有 3 個產品,則該消息出現 3 次、2 個產品、2 次。此外,單擊確認按鈕后不會刪除該產品。謝謝           $form = HTML::form('cart_quantity', CLICSHOPPING::link(null, 'Cart&Update'), 'post', 'role="form" id="cart_quantity"', ['tokenize' => true]);           for ($i=0, $n=count($products); $i<$n; $i++) {              $products_name_url = $this->getProductsUrlRewrited()->getProductNameUrl($this->getProductID($products[$i]['id']));              $products_name = HTML::hiddenField('products_id[]', $products[$i]['id']);              $products_name .= HTML::link($products_name_url, $products[$i]['name']);    //no message    //          $trash = HTML::link($this->link(null, 'Cart&Delete&products_id=' . $products[$i]['id']), '<i class="fas fa-trash"></i>', 'alt="' . $this->getDef('button_remove')) . '&nbsp;&nbsp;&nbsp;'; ==> works fine but no message    //with message              $remove_product = ['params' => 'id="remove_product' . $products[$i]['id'] .'"'];              $trash = HTML::link($this->link(null, 'Cart&Delete&products_id=' . $products[$i]['id']), HTML::button(null, 'fas fa-trash',null, null, $remove_product)) . '&nbsp;&nbsp;&nbsp;';              $test = 'remove_product' . $products[$i]['id'];              $trash .= '<script>    var $button = $(\'#'. $test . '\')    $(function() {      $button.click(function () {        $(\'form\').submit(function () {          if (!window.confirm(\'Are you sure?\')) return false        })      })    })    </script>             ';    }?></form>
查看完整描述

1 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

首先,您遇到的問題是任何單擊的按鈕都會觸發事件,其次數與子 DOM 元素中的按鈕數一樣多。因此,您需要做什么才能獲得觸發事件的確切元素:


//with message

$product_id = 'remove_product' . $products[$i]['id'];

$remove_product = ['params' => 'id="'. $product_id .'"'];

$trash = HTML::link($this->link(null, 'Cart&Delete&products_id=' . $products[$i]['id']), HTML::button(null, 'fas fa-trash',null, null, $remove_product)) . '&nbsp;&nbsp;&nbsp;';



$but_id = $product_id;

echo "<script>

    var button = $('#". $but_id . "');

    $(document).on('click', button, function (event) {

        console.log('button clicked');

        //$('form').submit(function () {

          //if (!window.confirm('Are you sure?')) return false;

        //})

    })

    </script>         

    ";

$(document).on('click', 'button', function(e){

   alert(this.id);

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>

    <button id="product_delete1" data-id="1">Delete</button>

    <button id="product_delete2" data-id="2">Delete</button>

    <button id="product_delete3" data-id="3">Delete</button>

</form>


查看完整回答
反對 回復 2022-07-29
  • 1 回答
  • 0 關注
  • 114 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號