表单计算器脚本基本价格未加载 [英] Form Calculator Script Base Price Not Loading OnLoad

查看:81
本文介绍了表单计算器脚本基本价格未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有一个计算器来计算我的下拉选项选择.

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);

  }
});

,并在html中获取数字,我将其作为:

and in html to get the number out I have it as:

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

问题是,我的底价为60,但是当我进入页面时,其底价为零.只有当我做出选择时,价格才会上涨.我如何使基本价格从页面加载开始就出现?

problem is, i have the baseprice at 60, however when i get on the page, its zero. only when i make selections will the price go up. how do i get the baseprice to appear from the beginning at page load?

我需要它以基本价格开始,因为不需要这些选择,因此,如果我不选择这些价格,我将单击提交表单",它会提交零美元而不是60美元.

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.

推荐答案

 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.

这篇关于表单计算器脚本基本价格未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆