gpt4 book ai didi

基于表单答案的 Javascript 计算

转载 作者:可可西里 更新时间:2023-11-01 13:49:44 29 4
gpt4 key购买 nike

我创建了一个包含多个输入和单选按钮的 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 的各个组件是否放置在正确的位置,我也不确定这个计算的逻辑。单选按钮给我带来了一些问题。

最佳答案

我建议您看看 ng-model了解如何使用表单域。

Here是我之前编写的一个演示应用程序,应该可以让您了解如何根据表单输入进行计算。

您的 quoteGenerator 是一个没有任何 Angular 代码的 js 函数。将 $scope 注入(inject)函数或使用 controllerAs,创建 Controller 并删除所有 document.getElementsById 调用。

Here是你的代码我已经开始改进你的代码但我已经停止完成演示因为我不明白文件的计算应该如何工作。

关于基于表单答案的 Javascript 计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36703719/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com