简体   繁体   English

Javascript自定义价格计算器

[英]Javascript Custom Price Calculator

I have this below code to calculate a custom price based upon selection. 我有下面的代码来根据选择来计算自定义价格。 It works for stretched canvas, but i was wondering how to make it work for other options if a user selects it. 它适用于拉伸画布,但是我想知道如果用户选择它,如何使其适用于其他选项。 (eg Canvas Boards). (例如,画布板)。 If option equals "Canvas Boards" calculate custom price. 如果选项等于“画布板”,则计算自定义价格。 Many thanks, Deryn. 非常感谢,Deryn。

 function calculate_total() {
     var ttl = 0;
     var len = eval(document.forms['calc']['length'].value);
     var wid = eval(document.forms['calc']['width'].value);
     var thick = document.forms['calc']['frame'].value;
     var strOptions = e.options[e.selectedIndex].text;
     if(len.length == 0) {
         return;
     }
     if(wid.length == 0) {
         return;
     }
     if(thick == 0) {
         return;
     }
     ttl = (len + wid) * 2;
     //inches
     if((ttl >= 24) && (ttl <= 36)) {
         ttl *= .19;
     }
     if((ttl >= 37) && (ttl <= 56)) {
         ttl *= .20;
     }
     if((ttl >= 57) && (ttl <= 76)) {
         ttl *= .21;
     }
     if((ttl >= 77) && (ttl <= 92)) {
         ttl *= .22;
     }
     if((ttl >= 93) && (ttl <= 100)) {
         ttl *= .23;
     }
     switch(thick) {
     case "1.5":
         ttl;
         break;
     case "2":
         ttl;
         break;
     case "3":
         ttl;
         break;
     }
     document.getElementById('total').value = ttl.toFixed(2);
 } // end function definition
<form name="calc" id="calc">
    Length: <input type="text" name="length" id="length" onchange="calculate_total()" /> Inches <br />
    Width: <input type="text" name="width" id="width" onchange="calculate_total()" /> Inches <br />

    <select name="frame" onchange="calculate_total()">
        <option value="0" selected>Select below</option>
        <option value="1.5">Streched Canvas</option>
        <option value="2">Canvas Boards</option>
        <option value="3">Canvas Rolls</option>
        <option value="4">Primed Boards</option>
        <option value="5">Paintings/Printing Streching</option>
    </select>
    <br /><br />
    <b>Total:</b> $ <input id="total" name="total" readonly=true style="border: none" />
</form>

Complete edit: 完成编辑:

Example on jsfiddle jsfiddle上的示例

In the link provided above, you can see the example, with other function for every option picked from the list. 在上面提供的链接中,您可以看到示例,以及从列表中选取的每个选项的其他功能。 For now they just shows alert window, but you should know what to put there, to count it. 目前,它们仅显示警报窗口,但是您应该知道该放置什么,然后对其进行计数。

function check_which_selected() {
    selectedOption = document.forms['calc']['frame'].options;
    thick = +selectedOption[selectedOption.selectedIndex].value;
    ttl = 0;
    len = +document.forms['calc']['length'].value;
    wid = +document.forms['calc']['width'].value;

    if (len.length <= 0) return;
    if (wid.length <= 0) return;
    if (thick <= 0) return;

    if (thick == "0") return;
    if (thick == "1.5") calculate_total();
    if (thick == "2") calculate_total_canvas_boards();
    if (thick == "3") calculate_tota_canvas_rolls();
    if (thick == "4") calculate_total_primed_boards();
    if (thick == "5") calculate_total_paintings();
}

This function lets you check, what option is chosed, what values are put into text boxes. 此功能使您可以检查,选择了什么选项,在文本框中输入了什么值。 If some textbox is empty, or option is not picked, it wont call any function, otherwise, it will call proper function depending on the option chosed. 如果某些文本框为空,或者未选择选项,则它将不会调用任何函数,否则,它将根据所选的选项来调用适当的函数。 Variables taken from tetboxes, are scoped for both of these function, so their value is assigned in check_which_selected() and then they can be used inside other functions. 从tetbox中获取的变量的作用域是这两个函数,因此它们的值在check_which_selected()分配,然后可以在其他函数中使用。

You can also use your switch/case to define which function will be called. 您还可以使用switch/case定义将调用哪个函数。

Is that something you was looking for? 那是您要找的东西吗?

It's great that you are learning JavaScript. 您正在学习JavaScript真是太好了。 Keep at it! 继续吧!

Are you sure it's working for stretched boards? 确定它适用于拉伸板吗? I see some areas that can be improved, and others that are just plain broken. 我看到一些可以改进的地方,而另一些则是显而易见的。

First, you are referencing a variable, e , but that variable is never defined. 首先,您要引用变量e ,但从未定义该变量。 That will cause an error. 那会导致错误。 It looks like e is meant to refer to the frame selector. 看起来e旨在引用frame选择器。 But, since e is only used to create strOptions , and strOptions is never used, we can just delete that whole line and not even worry about e . 但是,由于e仅用于创建strOptions ,而从不使用strOptions ,因此我们可以删除整行,甚至不必担心e

Next, the use of eval is discouraged, especially the way you intend to. 接下来,不建议使用eval ,尤其是您打算使用的方法。 It looks like you are trying to get a number from a string. 似乎您正在尝试从字符串中获取数字。 Several points here: 这里有几点:

  • There are many easier, safer ways of turning a string into a number. 有许多更简单,更安全的方式将字符串转换为数字。 Personally, I'd just use the + operator to do it. 就个人而言,我只需要使用+运算符即可。 Eg, var n = +"10" . 例如, var n = +"10" parseFloat() is another good option. parseFloat()是另一个不错的选择。
  • If the user does not enter a number, you will most likely get an error. 如果用户未输入数字,则很可能会收到错误消息。 For example, if the user enters "n/a", eval() will try to interpret that as script. 例如,如果用户输入“ n / a”,则eval()将尝试将其解释为脚本。 Assuming n and a are not actual variable names, that will cause an error. 假设na不是实际的变量名,则将导致错误。 But actually, it could be valid JavaScript: If you have variables n and a defined, and they are numbers, this actually will run and not return an error. 但是实际上,它可能是有效的JavaScript:如果您有变量na define,并且它们是数字,则实际上它将运行并且不会返回错误。 It will however give unexpected results - it will proceed as if the user entered the actual value that is n divided by a . 但是, 它将产生意想不到的结果-就像用户输入的n实际值除以a
  • The user can use your form to execute arbitrary script. 用户可以使用您的表单执行任意脚本。 Try entering alert("hello world") into one of your form fields. 尝试在您的一个表单字段中输入alert("hello world") Of course, the user can also just use the address bar or the browser's JavaScript console to execute arbitrary script, so this isn't really a security hole, but still, you don't want that. 当然,用户也可以只使用地址栏或浏览器的JavaScript控制台上执行任意脚本,所以这不是一个真正的安全漏洞,不过,你不希望出现这种情况。

The way you are accessing your form is more verbose than you need. 访问表单的方式比您需要的更为冗长。 Instead of: 代替:

document.forms['calc']['length'].value;

You can skip the square bracket notation and the reference to forms : 您可以跳过方括号符号和对forms的引用:

document.calc.length.value;

I'd go one further and just store a reference to document.calc in a variable: 我再进一步,只将对document.calc的引用存储在变量中:

var f = document.calc;
var len = +f.length.value;
var wid = +f.width.value;
var thick = +f.frame.value;

(Notice how I used + to get a number, as I mentioned earlier) (请注意,如前所述,请注意我如何使用+来获取数字)

Guessing here, it looks like you are applying a discount with your ttl *= .19 code, but it looks like you are applying it backwards. 在这里猜测,看起来您正在使用ttl *= .19代码应用折扣,但是看起来您正在向后应用折扣。 Eg, the more canvas your user purchases the higher the cost per inch. 例如,用户购买的画布越多,每英寸的成本就越高。 Shouldn't it be cheaper per inch the more you purchase? 您购买的数量越多,每英寸的价格不应该便宜吗? You know, like breakfast cereal? 你知道,像早餐麦片吗?

Either way, there are some flaws in the logic. 无论哪种方式,逻辑上都有一些缺陷。 You are always using less than or equal to and greater than or equal to. 您始终使用小于或等于且大于或等于。 Never just plain less than or greater than. 永远不要仅仅小于或大于。 So you are missing some values. 因此,您缺少一些值。 If I enter length 5 and width 5, my price is $20. 如果我输入长度5和宽度5,则我的价格是20美元。 But, if I enter length 6 and width 6 my prices is just $4.56. 但是,如果我输入长度6和宽度6,则我的价格仅为4.56美元。 If I enter 8 x 10, I get $6.84, but if I enter 8x10.25, I get $36.50. 如果输入8 x 10,我将得到$ 6.84,但是如果输入8x10.25,我将得到$ 36.50。 That can't be right, can it? 那是不对的,可以吗? Instead, do it like this: 而是这样做:

if (ttl > 92) {
    ttl *= .23;
}
else if (ttl > 76) {
    ttl *= .22;
}
else if (ttl > 56) {
    ttl *= .21;
}
else if (ttl > 36) {
    ttl *= .20;
}
else {
    ttl *= .19;
}

Your switch statement does absolutely nothing. 您的switch语句绝对不执行任何操作。 You can just remove that whole code block. 您可以删除整个代码块。

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

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