gpt4 book ai didi

javascript if else 使用数学为字段添加值不起作用

转载 作者:行者123 更新时间:2023-12-03 03:30:42 25 4
gpt4 key购买 nike

无法让它发挥作用,盯着它看瞎了。有任何想法吗?如果值 kg 高于 * 的乘积,则尝试将值 kg 放入 lbh 字段中m3总计 (l*****b*****h/100000/00035)*。它用于计算货件的运费。

它用于 if else 中的计算,我想我在这里遗漏了一些东西。

function add() {
var m3 = parseInt(document.getElementById("m3").value);
var kg = parseInt(document.getElementById("kg").value);
var l = parseInt(document.getElementById("L").value);
var b = parseInt(document.getElementById("B").value);
var h = parseInt(document.getElementById("H").value);
var x = 0.0035;
var y = 3350;


var total = 0;
kg = +kg || 0
m3 = +m3 || 0
l = +l || 0
b = +b || 0
h = +h || 0
total = Math.ceil((m3total / 0.0035));
kgtotal = (kg + 0)
m3total = (l * b * h) / 1000000;
(document.getElementById("m3").value) = m3total;
}
if ((total > kgtotal)) {

(document.getElementById("LBvekt").value) = total;

} else if ((total < kgtotal)) {
(document.getElementById("LBvekt").value) = kgtotal;
}
<p>Click the button</p>

<button onclick="add()">Try it</button>

<p id="demo"></p>


<div class="form-group">
<div class="col-sm-6 col-sm-offset-3">
<div class="row gutter-sm">


<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" id="kg" name="kg" placeholder="KG" onChange="add()">
</div>
<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" id="m3" name="m3" placeholder="m3" readonly>
</div>
<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" name="LBvekt" id="LBvekt" placeholder="LBvekt">
</div>

</div>
</div>
</div>

<div class="form-group">
<div class="col-sm-6 col-sm-offset-3">
<div class="row gutter-sm">


<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" id="L" name="L" placeholder="L (cm)">
</div>
<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" id="B" name="B" placeholder="B (cm)">
</div>
<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" id="H" name="H" placeholder="H (cm)" onChange="add()">
</div>

</div>
</div>
</div>

最佳答案

假设代码片段中的代码是您的实际代码,您的 if 语句仅执行一次:页面加载时。它位于 add() 函数之外,因此仅在加载时执行。

您很可能希望将 if 语句放在 add() 函数中,如下所示:

function add() {
var m3 = parseInt(document.getElementById("m3").value);
var kg = parseInt(document.getElementById("kg").value);
var l = parseInt(document.getElementById("L").value);
var b = parseInt(document.getElementById("B").value);
var h = parseInt(document.getElementById("H").value);
var x = 0.0035;
var y = 3350;


var total = 0;
kg = +kg || 0
m3 = +m3 || 0
l = +l || 0
b = +b || 0
h = +h || 0
m3total = (l * b * h) / 1000000;
total = Math.ceil((m3total / 0.0035));
kgtotal = (kg + 0)

(document.getElementById("m3").value) = m3total;
if ((total > kgtotal)) {

(document.getElementById("LBvekt").value) = total;

} else if ((total < kgtotal)) {
(document.getElementById("LBvekt").value) = kgtotal;
}
}
<p>Click the button</p>

<button onclick="add()">Try it</button>

<p id="demo"></p>


<div class="form-group">
<div class="col-sm-6 col-sm-offset-3">
<div class="row gutter-sm">


<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" id="kg" name="kg" placeholder="KG" onChange="add()">
</div>
<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" id="m3" name="m3" placeholder="m3" readonly>
</div>
<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" name="LBvekt" id="LBvekt" placeholder="LBvekt">
</div>

</div>
</div>
</div>

<div class="form-group">
<div class="col-sm-6 col-sm-offset-3">
<div class="row gutter-sm">


<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" id="L" name="L" placeholder="L (cm)">
</div>
<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" id="B" name="B" placeholder="B (cm)">
</div>
<div class="col-sm-3 m-b-15 m-sm-b-0">
<input type="text" class="form-control" id="H" name="H" placeholder="H (cm)" onChange="add()">
</div>

</div>
</div>
</div>

关于javascript if else 使用数学为字段添加值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46122134/

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