我需要将下面的结果代码放到我的结果框中,该代码正在运行而没有html界面。 这正是我想要实现的 ●2个输入,用于比例的2个元素 ●用于计算所需平衡重量的按钮 ●显示结果的div
function pushData() {
var w1, w2, res;
var weights = [1, 2, 7, 7];
w1 = Number(document.scalebalancing.txtnum1.value);
w2 = Number(document.scalebalancing.txtnum2.value);
for (var i = 0; i < weights.length; i++) {
if (w1 + weights[i] === w2 || w2 + weights[i] === w1) {
return '' + weights[i];
}
for (var j = i + 1; j < weights.length; j++) {
if (w1 + weights[i] + weights[j] === w2 || w2 + weights[i] + weights[j] === w1 || w1 + weights[i] === w2 + weights[j] || w2 + weights[i] === w1 + weights[j]) {
return '' + weights[i] + ',' + weights[j];
}
}
}
console.log(pushData());
res = pushData();
document.scalebalancing.txtres.value = res;
return 'Scale Imbalanced';
}
<html>
<head>
<title>Scale Balancing</title>
</head>
<body>
<form name="scalebalancing">
Number 1: <input type="text" name="txtnum1">
<br> Number 2: <input type="text" name="txtnum2">
<br> Result : <input type="text" name="txtres"><br>
<input type="button" value="Calculate" onClick="pushData()">
</form>
</body>
</html>
答案 0 :(得分:0)
代码示例显示不完整。 (表单顶部缺少)。
尝试使用w1 = Number(document.querySelector('input[name="txtnum1"]').value)
作为获取输入值的一种方法。
答案 1 :(得分:0)
首先,您需要提供元素ID,这将使您能够引用这些值。在代码段中,我分配了与名称相同的ID,并使用document.getElementByID
来获取值。同样,您无需在脚本中第二次调用该函数(就像您在分配res值时所做的那样)。该函数已被调用,因此在进行计算的脚本期间将值分配给res,否则会出现错误。 (撰写答案时出现“最大堆栈”错误-以为是我的缓存一分钟,然后清除了它!)
希望这会有所帮助
function pushData() {
var w1, w2, res;
var weights = [1, 2, 7, 7];
w1 = Number(document.getElementById("txtnum1").value);
w2 = Number(document.getElementById("txtnum2").value);
for (var i = 0; i < weights.length; i++) {
if (w1 + weights[i] === w2 || w2 + weights[i] === w1) {
res = '' + weights[i];
}
for (var j = i + 1; j < weights.length; j++) {
if (w1 + weights[i] + weights[j] === w2 || w2 + weights[i] + weights[j] === w1 || w1 + weights[i] === w2 + weights[j] || w2 + weights[i] === w1 + weights[j]) {
res = '' + weights[i] + ',' + weights[j];
}
}
}
console.log(res);
//res = pushData();
document.getElementById("txtres").value = res;
return 'Scale Imbalanced';
}
<html>
<head>
<title>Scale Balancing</title>
</head>
<body>
<form id ="calc" name="scalebalancing">
Number 1: <input type="text" id="txtnum1" name="txtnum1">
<br> Number 2: <input type="text" id="txtnum2" name="txtnum2">
<br> Result : <input type="text" id ="txtres" name="txtres"><br>
<input type="button" value="Calculate" onClick="pushData()">
</form>
</body>
</html>
答案 2 :(得分:0)
我看到您的函数中有return语句,这些语句根本不让您的赋值语句执行。
for (var i = 0; i < weights.length; i++) {
if (w1 + weights[i] === w2 || w2 + weights[i] === w1) { return '' + weights[i]; }
for (var j = i + 1; j < weights.length; j++) {
if (w1 + weights[i] + weights[j] === w2 || w2 + weights[i] + weights[j] === w1 || w1 + weights[i] === w2 + weights[j] || w2 + weights[i] === w1 + weights[j]) {
return '' + weights[i] + ',' + weights[j];
}
}
}
更改返回值,将其分配给“ res”,这将为您解决问题。
for (var i = 0; i < weights.length; i++) {
if (w1 + weights[i] === w2 || w2 + weights[i] === w1) {
res = '' + weights[i];
}
for (var j = i + 1; j < weights.length; j++) {
if (w1 + weights[i] + weights[j] === w2 || w2 + weights[i] + weights[j] === w1 || w1 + weights[i] === w2 + weights[j] || w2 + weights[i] === w1 + weights[j]) {
res = weights[i] + ',' + weights[j];
}
}
}
请检查小提琴以获取更新的代码