jQuery development of shopping cart function implementation

In this section we will use jQuery code to implement various functional modules of the shopping cart.

Includes the functions of selecting all, inverting selection, and canceling the click check box of the product.

Use <input id="Checkbox1" type="checkbox" class="allselect"/> check box to perform all-select operation, use click event, when we click

When selecting all, all <input type="checkbox"> are selected. Inverse selection means that all selections are selected by default, and then all selections are deselected when we click. The cancellation operation is also

similar. The difference is that when there is no selection, clicking cancel means selecting; when there is selection, clicking cancel means not selecting.

<script type="text/javascript">
$(document).ready(function () {
      //jquery特效制作复选框全选反选取消(无插件)
      // 全选
      $(".allselect").click(function () {
         if(this.checked){
            $(".gwc_tb2 input[name=newslist]").prop("checked",true);
         } else{
            $(".gwc_tb2 input[name=newslist]").prop("checked",false);
            $(this).next().css({ "background-color": "#3366cc", "color": "#ffffff" });
         }
         GetCount();
      });

      //反选
      $("#invert").click(function () {
         $(".gwc_tb2 input[name=newslist]").each(function () {
            if ($(this).prop("checked")) {
               $(this).prop("checked", false);
               $(this).next().css({ "background-color": "#ffffff", "color": "#000000" });
            } else {
               $(this).prop("checked", true);
               $(this).next().css({ "background-color": "#3366cc", "color": "#000000" });
            }
         });
         GetCount();
      });

      //取消
      $("#cancel").click(function () {
         $(".gwc_tb2 input[name=newslist]").each(function () {
            $(this).prop("checked", false);
            $(this).next().css({ "background-color": "#ffffff", "color": "#000000" });
         });
         GetCount();
      });

      // 所有复选(:checkbox)框点击事件
      $(".gwc_tb2 input[name=newslist]").click(function () {
         if ($(this).prop("checked")) {
            $(this).next().css({ "background-color": "#3366cc", "color": "#ffffff" });
         } else {
            $(this).next().css({ "background-color": "#ffffff", "color": "#000000" });
         }
      });

      // 输出
      $(".gwc_tb2 input[name=newslist]").click(function () {
         GetCount();     
      });
   });
   //获取数量
   function GetCount() {
      var conts = 0;
      var aa = 0;
      $(".gwc_tb2 input[name=newslist]").each(function () {
         if ($(this).prop("checked")) {
            for (var i = 0; i < $(this).length; i++) {
               conts += parseInt($(this).val());
               aa += 1;
            }
         }
      });
      $("#shuliang").text(aa);
      $("#zong1").html((conts).toFixed(2)); //toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。
      $("#jz1").css("display", "none");
      $("#jz2").css("display", "block");
   }
</script>

The product quantity increase and decrease function automatically calculates the subtotal price of products by clicking on the increase or decrease of the product.

Set the id by <input id=""> to operate. When the "+" button is clicked, the product quantity is +1. When the "-" button is clicked, the product quantity is -1. . The item subtotal price and total price will also change accordingly.

There is something that friends need to pay attention to. When the quantity of goods is reduced to 0, clicking the "-" button will cause the quantity of goods to be negative, which is unreasonable.

So when the product quantity is 0, click the "-" button, the product quantity will no longer be -1, and the total product price page will no longer be reduced.

<!---商品加减算总数---->
<script type="text/javascript">
   $(function () {
      var t = $("#text_box1");
      $("#add1").click(function () {
         t.val(parseInt(t.val()) + 1);
         setTotal(); GetCount();
      });
      $("#min1").click(function () {
         if(parseInt(t.val() - 1) < 0){
            return false;
         }else {
            t.val(parseInt(t.val()) - 1);
         }
         setTotal(); GetCount();
      });
      function setTotal() {
         $("#total1").html((parseInt(t.val()) * 9).toFixed(2));
         $("#newslist-1").val(parseInt(t.val()) * 9);
      }
      setTotal();
   })
</script>

Calculate the total quantity and total price of the selected goods by clicking to select

<!---总数---->
<script type="text/javascript">
   $(function () {
      $(".quanxun").click(function () {
         setTotal();
         //alert($(lens[0]).text());
      });
      function setTotal() {
         var len = $(".tot");
         var num = 0;
         for (var i = 0; i < len.length; i++) {
            num = parseInt(num) + parseInt($(len[i]).text());
         }
         //alert(len.length);
         $("#zong1").text(parseInt(num).toFixed(2));
         $("#shuliang").text(len.length);
      }
      //setTotal();
   })
</script>

Note:

length Attributes can be set Or return the number of elements in the array

Continuing Learning
||
<script type="text/javascript"> $(function () { var t = $("#text_box1"); $("#add1").click(function () { t.val(parseInt(t.val()) + 1); setTotal(); GetCount(); }); $("#min1").click(function () { if(parseInt(t.val() - 1) < 0){ return false; }else { t.val(parseInt(t.val()) - 1); } setTotal(); GetCount(); }); function setTotal() { $("#total1").html((parseInt(t.val()) * 9).toFixed(2)); $("#newslist-1").val(parseInt(t.val()) * 9); } setTotal(); }) </script>
submitReset Code