简体   繁体   English

表单计算器脚本基本价格未加载 OnLoad

[英]Form Calculator Script Base Price Not Loading OnLoad

I have a calulcator in my form to calculate my dropdown options choices.我的表单中有一个计算器来计算我的下拉选项选择。

$(function() {
  $("select.calculate").on("change", calc);
  $("input[type=checkbox].calculate").on("click", calc);

  function calc() {
    var basePrice = 60;
    newPrice = basePrice;
    $("select.calculate option:selected, input[type=checkbox].calculate:checked").each(function() {
      newPrice += parseInt($(this).data('price'), 10);
    });

    newPrice = newPrice.toFixed(2);
    $("#item-price").html(newPrice);
    $("#item_price_val").val(newPrice);

  }
});

and in html to get the number out I have it as:并在 html 中获取数字,我将其设为:

<span id="item-price">0</span>

problem is, i have the baseprice at 60, however when i get on the page, its zero.问题是,我的基本价格是 60,但是当我进入页面时,它是零。 only when i make selections will the price go up.只有当我做出选择时,价格才会上涨。 how do i get the baseprice to appear from the beginning at page load?我如何在页面加载时从一开始就显示基本价格?

I need it to start as base price as those selections are not required, so if I do not choose those, i will click submit form, and it submits zero dollars instead of 60 dollars.我需要它作为基本价格开始,因为这些选择不是必需的,所以如果我不选择那些,我将点击提交表单,它提交零美元而不是 60 美元。

 var basePrice = 60; 
 $(document).ready(function() {
 $("select.calculate").on("change", calc);
 $("input[type=checkbox].calculate").on("click", calc);
 $("#item-price").html(basePrice);
 $("#item_price_val").val(basePrice);
});
function calc() {
newPrice = basePrice;
$("select.calculate option:selected,input[type=checkbox].calculate:checked").each(function() {
  newPrice += parseInt($(this).data('price'), 10);
});
newPrice = newPrice.toFixed(2);
$("#item-price").html(newPrice);
$("#item_price_val").val(newPrice);
}

Please Use an updated version of your code.请使用您的代码的更新版本。

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

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