jq、js选择物品实时计算价格

<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
  </head>

  <body>
    <div>
      <input type="radio" name="carType" id="" value="100" />
      <input type="radio" name="carType" id="" value="200" />
      <input type="radio" name="carType" id="" value="300" />
      <input type="radio" name="carType" id="" value="400" />
    </div>
    <div>
      <input type="radio" name="carColor" id="" value="100" />
      <input type="radio" name="carColor" id="" value="200" />
      <input type="radio" name="carColor" id="" value="300" />
      <input type="radio" name="carColor" id="" value="400" />
    </div>
    <div>
      <input type="radio" name="carPz" id="carPz" value="100" />
      <input type="radio" name="carPz" id="carPz" value="200" />
      <input type="radio" name="carPz" id="carPz" value="300" />
      <input type="radio" name="carPz" id="carPz" value="400" />
    </div>
    <div>
      <input type="radio" name="carXp" id="" value="100" />
      <input type="radio" name="carXp" id="" value="200" />
      <input type="radio" name="carXp" id="" value="300" />
      <input type="radio" name="carXp" id="" value="400" />
    </div>
    <button class="math">计算</button>
    <div style="border: 1px solid black; height: 20px;">
      <span>价格: </span> <span class="priceLine"> 0 </span>
    </div>
    <script type="text/javascript">
      $("input").attr("onclick", "mathPrice()");
      var carType = undefined;
      var carColor = undefined;
      var carPz = undefined;
      var carXp = undefined;

      function mathPrice() {
        $(".priceLine").text("");
        //获取checked项的value值
        carType = $('input[name="carType"]:checked').val();
        carColor = $('input[name="carColor"]:checked').val();
        carPz = $('input[name="carPz"]:checked').val();
        carXp = $('input[name="carXp"]:checked').val();
        //判断并做兼容处理并转化为数字
        var carTypePrice = carType == undefined ? 0 : carType * 1;
        var carColorPrice = carColor == undefined ? 0 : carColor * 1;
        var carPzPrice = carPz == undefined ? 0 : carPz * 1;
        var carXpPrice = carXp == undefined ? 0 : carXp * 1;
        //总价格相加
        var allPrice = carTypePrice + carColorPrice + carPzPrice + carXpPrice;
        $(".priceLine").text(allPrice);
      }
      //点击按钮提交的总价
      $(".math").on("click", function () {
        if (
          carType == undefined ||
          carColor == undefined ||
          carPz == undefined ||
          carXp == undefined
        ) {
          alert("请全部选择后提交!");
        } else {
          var carPrice = $(".priceLine").text();
          alert(carPrice);
        }
      });
    </script>
  </body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值