基于表单答案的Javascript计算

时间:2016-04-18 20:30:00

标签: javascript html angularjs

我创建了一个包含多个输入和单选按钮的Web表单。需要收集每个答案来计算成本。我不确定我的JS逻辑(特别是与单选按钮有关)是否正确。有人可以看看这个,看看我是否正确这样做?谢谢

HTML:

<div class="row">
    <div class="form-group col-md-4">
        <label for="company">Company Name*</label><br />
        <input type="text" class="form-control" name="company" placeholder="Company Name" ng-model="user.company">

    </div>
    <div class="form-group col-md-4">
        <label for="salesp">Salesperson Name*</label><br />
        <input type="text" class="form-control" name="salesp" placeholder="Salesperson Name" ng-model="user.salesp">

    </div>
    <div class="form-group col-md-4">
        <label for="emailsales">Contact Email*</label><br />
        <input type="email" class="form-control" name="emailsales" placeholder="john.doe@example.com" ng-model="user.emailsales">

    </div>
</div>
<div class="row">
    <div class="form-group col-md-4">
        <label for="partnum">Number of Part Being Made(Finished Goods)</label>
        <input type="number" class="form-control" id ="partnum" name="partnum" placeholder="Enter # items" ng-model="user.partnum">

    </div>
    <div class="form-group col-md-4">
        <label>New Files?</label><br />
        <input type="radio" name="files" id="files" value="42" ng-model="user.files"> Yes
        <input type="radio" name="files" id="files" value="0"ng-model="user.files"> No

    </div>
    <div class="form-group col-md-4">
        <label>Rerun Files?</label><br />
        <input type="radio" name="oldfiles" value="15" ng-model="user.oldfiles"> Yes
        <input type="radio" name="oldfiles" value="0" ng-model="user.oldfiles"> No

    </div>
</div>
<div class="row">
    <div class="form-group col-md-4">
        <label for="shortp">Shortest Dimension on Print Part (inches)</label>
        <input type="number" class="form-control" name="shortp" placeholder="Shortest Dim" ng-model="user.shortp">

    </div>
    <div class="form-group col-md-4">
        <label for="longp">Longest Dimension on Print Page (inches)</label>
        <input type="number" class="form-control" name="longp" placeholder="Longest Dim" ng-model="user.longp">

    </div>
    <div class="form-group col-md-4">
        <label for="pressnum">Number of Ups on Press Sheet</label>
        <input type="number" class="form-control" name="pressnum" placeholder="# Ups" ng-model="user.pressnum">

    </div>
</div>
<div class="row">
    <div class="form-group col-md-4">
        <label for="shortpress">Number of Short Press Sheet</label>
        <input type="number" class="form-control" id="shortpress" name="shortpress" placeholder="# Short Press" ng-model="user.shortpress">

    </div>
    <div class="form-group col-md-4">
        <label for="longpress">Number of Long Press Sheet</label>
        <input type="number" class="form-control" id="longpress" name="longpress" placeholder="# Long Press" ng-model="user.longpress">

    </div>
    <div class="form-group col-md-4">
        <label>Passes First Side?</label><br />
        <input type="radio" name="passfirst" value="Yes" ng-model="user.passfirst"> Yes
        <input type="radio" name="passfirst" value="No" ng-model="user.passfirst"> No

    </div>
</div>
<div class="row">
    <div class="form-group col-md-4">
        <label for="inkcoverone">Percent of Ink Coverage on First Side</label>
        <input type="number" class="form-control" id="inkcoverone" name="inkcoverone" placeholder="% Ink Coverage" ng-model="user.inkcoverone">

    </div>
    <div class="form-group col-md-4">
        <label>Passes Second Side?</label><br />
        <input type="radio" name="passsecond" value="1" ng-model="user.passsecond"> Yes
        <input type="radio" name="passsecond" value="0" ng-model="user.passsecond"> No

    </div>
    <div class="form-group col-md-4">
        <label for="inkcovertwo">Percent of Ink Coverage on Second Side</label>
        <input type="number" class="form-control" name="inkcovertwo" placeholder="% Ink Coverage" ng-model="user.inkcovertwo">

    </div>
</div>
<div class="row">
                <div class="form-group col-md-offset-10">
                    <button type="submit" class="btn btn-default btn-block register" ng-click="quoteGenerator()"> GET QUOTE </button>
                </div>
</div>

这是JS:

function quoteGenerator(){
    let count = parseInt(document.getElementById('partnum').value);
    if (count > 0) {
        let size = 0;
        for (var i = 0 ; i < document.getElementsByName('files').length; i++) {
            if (document.getElementsByName('files')[i].checked) {
                    size = parseInt( document.getElementsByName('files')[i].value);
                    break;
            }
            if(size == 42){
                let newcount = count * size;
            } else if(size==0){
                for (var i = 0 ; i < document.getElementsByName('oldfiles').length; i++) {
                    if (document.getElementsByName('oldfiles')[i].checked) {
                        newsize = parseInt( document.getElementsByName('oldfiles')[i].value);
                        break;
                    }
                }
                let newcount= count*newsize;
            }
        }       
    }
    var ink = parseInt(document.getElementById('inkcoverone').value);
    var shortp = parseInt(document.getElementById('shortpress').value);
    var longp = parseInt(document.getElementById('longpress').value);
    var parentsheet = parseInt(document.getElementById('psheetnum').value);
    let inkcost1 = ((longp+shortp)/144)*.19*ink*parentsheet;

    for (var i = 0 ; i < document.getElementsByName('passsecond').length; i++) {
            if (document.getElementsByName('passsecond')[i].checked) {
                    pass2 = parseInt( document.getElementsByName('passsecond')[i].value);
                    break;
            }
    }
    var ink2 = parseInt(document.getElementById('inkcovertwo').value);
    let inkcost2 = ((longp+shortp)/144)*.19*ink2*parentsheet*pass2;

}

方程式都基于HTML表单中变量的id。方程式都是正确的,我只是不确定totalCost的各个组件的位置是否放在正确的位置,我也不确定这个计算的逻辑。单选按钮给我带来了一些问题。

1 个答案:

答案 0 :(得分:1)

我建议您查看ng-model,了解如何使用表单字段。

Here是我之前编写的一个演示应用,应该让您知道如何根据表单输入进行计算。

您的quoteGenerator是一个没有任何角度代码的js函数。将$scope注入函数或使用controllerAs,创建一个控制器并删除所有document.getElementsById次调用。

Here是您的代码我已经开始改进您的代码但我已经停止完成演示,因为我不明白如何使用这些文件进行计算。