今日は、 html構造のコードはすべて記事の最後にありますので、わかる人はわかるでしょう! ! ! 1. すべて選択
以上が実現したい基本機能のデモンストレーションです。次はケーススタディに書いていきたいと思います。 // すべて選択 $(".checkall").change(function () { $(".j-checkbox,.checkall").prop("チェック済み", $(this).prop("チェック済み")) }); $(".j-checkbox").change(関数() { ($(".j-checkbox:checked").length === $(".j-checkbox").length) の場合 { $(".checkall").prop("チェック済み", true); } それ以外 { $(".checkall").prop("チェック済み", false); } }); 2. 商品の数量を増やすか減らす
数量を増やすにはプラス記号をクリックし、数量を減らすにはマイナス記号をクリックします。これは加算機能と減算機能の基本的な実装であり、 プラス記号とマイナス記号にそれぞれクリック イベントを与え、フォーム内の値を取得し、変数を使用して増減し、内部の値を変更します。 // 数量プラス $(".increment").click(function () { var n = $(this).siblings(".itxt").val(); n++; $(this).siblings(".itxt").val(n); }) // 数量の削減$(".decrement").click(function () { var n = $(this).siblings(".itxt").val(); (n == 1)の場合{ false を返します。 } n--; $(this).siblings(".itxt").val(n); }); 3. 商品の小計を変更する
小計の値はプラスまたはマイナス記号をクリックすると変更されるため、ここでのコードはプラスまたはマイナス記号のクリックイベントに記述する必要があります。このイベントをクリックした後、単価の値を取り出し、数量の値と掛け合わせて合計価格を取得します。以下は、全体のコードの基本的な実装プロセス案です。 // 数量プラス $(".increment").click(function () { var n = $(this).siblings(".itxt").val(); n++; $(this).siblings(".itxt").val(n); // 商品の小計を変更します。 var p = $(this).parents(".p-num").siblings(".p-price").html(); p = p.substr(1); var 価格 = (p * n).toFixed(2); $(this).parent().parent().siblings(".p-sum").html("¥" + 価格); 合計を取得します。 }) // 数量の削減$(".decrement").click(function () { var n = $(this).siblings(".itxt").val(); (n == 1)の場合{ false を返します。 } n--; $(this).siblings(".itxt").val(n); // 商品の小計を変更します。 var p = $(this).parents(".p-num").siblings(".p-price").html(); p = p.substr(1); var 価格 = (p * n).toFixed(2); $(this).parent().parent().siblings(".p-sum").html("¥" + 価格); 合計を取得します。 }); 4. 合計と合計額を計算する
本質的には、チェックボックスを選択すると、商品が決済列にカウントされます。ここでは、ショッピングカートの数量と小計の合計を記入します。数量が変わると小計も変わり、それに応じて決済列のデータにも影響します。 // 決済された商品の数と価格をカプセル化します getSum(); 関数 getSum() { var カウント = 0; var お金 = 0; $(".itxt").each(関数(i, ele) { count += parseInt($(ele).val()); }); $(".amount-sum em").text(count); $(".p-sum").each(関数(i, ele) { お金 += parseFloat($(ele).text().substr(1)) }); $(".price-sum em").text("¥" + money.toFixed(2)); } 5. 製品を削除する
商品の後ろにある // 削除 // $(".p-action a").click(function () {の後ろの項目を削除します $(this).parents(".cart-item").remove(); 合計を取得します。 }); // 選択した製品を削除します$(".remove-batch").click(function () { $(".j-checkbox:checked").parents(".cart-item").remove(); 合計を取得します。 }) // ショッピングカートをクリアする $(".clear-all").click(function () { $(".cart-item").remove(); 合計を取得します。 }) 6. 選択した商品に背景を追加する
背景色は選択されたときに追加されるので、チェックボックスが変更された場合にここにコードを記述する必要があります。 // すべて選択 $(".checkall").change(function () { $(".j-checkbox,.checkall").prop("チェック済み", $(this).prop("チェック済み")) // 製品に背景色を追加します if ($(this).prop("checked")) { $(".cart-item").addClass("チェックカートアイテム"); } それ以外 { $(".cart-item").removeClass("チェックカートアイテム"); } }); $(".j-checkbox").change(関数() { ($(".j-checkbox:checked").length === $(".j-checkbox").length) の場合 { $(".checkall").prop("チェック済み", true); } それ以外 { $(".checkall").prop("チェック済み", false); } // 製品に背景色を追加します if ($(this).prop("checked")) { $(this).parents(".cart-item").addClass("チェックカートアイテム"); } それ以外 { $(this).parents(".cart-item").removeClass("チェックカートアイテム"); } }); 7. HTMLのすべてのコアコード<div class="c-container"> <div class="w"> <div class="カートフィルターバー"> <em>すべての製品</em> </div> <!-- ショッピングカートのメインコア領域 --> <div class="cart-warp"> <!-- ヘッダー すべてのモジュールを選択 --> <div class="cart-thead"> <div class="t-checkbox"> <input type="checkbox" name="" id="" class="checkall"> すべて選択</div> <div class="t-goods">商品</div> <div class="t-price">単価</div> <div class="t-num">数量</div> <div class="t-sum">小計</div> <div class="t-action">アクション</div> </div> <!-- 製品詳細モジュール--> <div class="カートアイテムリスト"> <div class="カートアイテムチェックカートアイテム"> <div class="p-checkbox"> <input type="checkbox" name="" id="" チェック済み class="j-checkbox"> </div> <div class="p-goods"> <div class="p-img"> <img src="upload/p1.jpg" alt=""> </div> <div class="p-msg">【5冊で26.8元】児童文学の古典カラーイラスト青少年版『八十日間世界一周』中学生中国語教育カリキュラム</div> </div> <div class="p-price">¥12.60</div> <div class="p-num"> <div class="数量フォーム"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="decrement">-</a> <入力タイプ="テキスト" クラス="itxt" 値="1"> <a href="javascript:;" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" class="increment">+</a> </div> </div> <div class="p-sum">¥12.60</div> <div class="p-action"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >削除</a></div> </div> <div class="cart-item"> <div class="p-checkbox"> <input type="チェックボックス" name="" id="" class="j-checkbox"> </div> <div class="p-goods"> <div class="p-img"> <img src="upload/p2.jpg" alt=""> </div> <div class="p-msg">[2000 ステッカー] ステッカーブック 3-6 歳 ステッカー 子供用 ステッカーブック 12 冊セット ステッカー 子供用 車</div> </div> <div class="p-price">¥24.80</div> <div class="p-num"> <div class="数量フォーム"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="decrement">-</a> <入力タイプ="テキスト" クラス="itxt" 値="1"> <a href="javascript:;" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" class="increment">+</a> </div> </div> <div class="p-sum">¥24.80</div> <div class="p-action"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >削除</a></div> </div> <div class="cart-item"> <div class="p-checkbox"> <input type="チェックボックス" name="" id="" class="j-checkbox"> </div> <div class="p-goods"> <div class="p-img"> <img src="upload/p3.jpg" alt=""> </div> <div class="p-msg">唐詩三百篇+慣用句物語(全2巻)1年生課外学習図書 ハードカバー 音声表記 児童版 小学生2年生と3年生の課外学習図書</div> </div> <div class="p-price">¥29.80</div> <div class="p-num"> <div class="数量フォーム"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="decrement">-</a> <入力タイプ="テキスト" クラス="itxt" 値="1"> <a href="javascript:;" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" rel="外部 nofollow" class="increment">+</a> </div> </div> <div class="p-sum">¥29.80</div> <div class="p-action"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >削除</a></div> </div> </div> <!-- 決済モジュール--> <div class="カートフロートバー"> <div class="すべて選択"> <input type="checkbox" name="" id="" class="checkall">すべて選択</div> <div class="操作"> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="remove-batch"> 選択した商品を削除</a> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="clear-all">ショッピングカートをクリア</a> </div> <div class="toolbar-right"> <div class="amount-sum"><em>1</em> 個のアイテムが選択されました</div> <div class="price-sum">合計金額: <em>¥12.60</em></div> <div class="btn-area">チェックアウトへ進む</div> </div> </div> </div> </div> </div> 上記は、jQuery を使用してすべてのショッピング カート機能を実装する方法の例です。お役に立てば幸いです。また、123WORDPRESS.COM ウェブサイトをサポートしてくださっている皆様にも感謝申し上げます。 以下もご興味があるかもしれません:
|
<<: CSSはボックスコンテナ(div)の高さを常に100%に設定します。
この記事では、VMware Workstation Pro で Win10 オペレーティング システ...
この記事では、配列を走査する 4 つの方法を比較してまとめます。 for ループ: for (let...
基本的なネットワーク構成Docker はイメージに基づいて複数のコンテナを「開く」ことができ、各コン...
Prometheus (プロメテウスとも呼ばれる) 公式サイト: https://prometheu...
Docker は 2013 年のリリース以来、広く注目され、ソフトウェア業界を変える可能性を秘めてい...
コードをコピーコードは次のとおりです。 <span style="font-size...
目次1. sysbenchの紹介#プロジェクトダウンロードアドレス: 2. Sysbenchのインス...
目次1. サーバー2. クライアント3. テストサービス1. サーバー1. YUMソースを使用してN...
1. nginxソースディレクトリに新しいrtmpディレクトリを作成し、git clone http...
MySQL エラー: パラメータ インデックスが範囲外です (1 > パラメータ数、つまり 0...
序文実際には、次のような問題に遭遇する可能性があります。特定のレコードの ID がわかっていて、その...
以前、raspbian で実行したときに opencv の一部の依存関係をパッケージ化できず、一部の...
戦争パッケージを準備する1. 既存のSpringBootプロジェクトを準備し、pomに依存関係を追加...
SQL Server のバッチコメントバッチ注釈Ctrl + (K, C): Ctrlキーを押しなが...
Navicat クライアントを通じてデフォルトの日時値を変更する際に問題が発生しました。データベース...