Problem: I'm trying to pass a return value from a function into a input field.
问题:我正在尝试将函数的返回值传递给输入字段。
I get no errors the field is just blank.
我没有错误,该字段只是空白。
Question: Why is my code not working?
问题:为什么我的代码不起作用?
HTML
<tr>
<td>Quantity to be dredged</td>
<td><input type="number" class="" id="shorePipe"></input></td>
</tr>
JavaScript
var shorePipe = function() {
var val = 100;
var val2 = 100;
var total = val * val2 / 100;
// return total;
document.getElementById('shorePipe').value = total;
};
Thanks in advance!
提前致谢!
4 个解决方案
#1
2
Since your code works perfectly fine (beside </input>
), the only issue I see here is that you're probably not calling shorePipe();
once the DOM is read and ready.
由于你的代码完全正常(在 旁边),我在这里看到的唯一问题是你可能没有调用shorePipe();一旦DOM被读取并准备就绪。
So put your JS right before the closing </body>
tag
所以把你的JS放在结束 标签之前
<script>
var shorePipe = function() {
var val = 100;
var val2 = 100;
var total = val * val2 / 100;
// return total;
document.getElementById('shorePipe').value = total;
};
shorePipe();
</script>
</body>
notice also that </input>
is invalid. <input>
is in the group of void elements and should not have a closing tag.
另请注意 无效。 在void元素组中,不应该有结束标记。
#2
0
You have to call the function
你必须调用该函数
shorePipe();
Here's a fiddle: https://jsfiddle.net/2vxdyvgz/
这是一个小提琴:https://jsfiddle.net/2vxdyvgz/
#3
0
- Input doesn't need to close itself.
- Make sure you actually call your JS function somewhere.
输入不需要自行关闭。
确保你实际上在某处调用了JS函数。
I created this JSFiddle for you: https://jsfiddle.net/pd12edbp/2/
我为你创建了这个JSFiddle:https://jsfiddle.net/pd12edbp/2/
<input type="number" class="" id="shorePipe" readyonly>
JS:
var shorePipe = function() {
var val = 100;
var val2 = 100;
var total = val * val2 / 100;
// return total;
document.getElementById('shorePipe').value = total;
};
$(document).ready(function(){
shorePipe();
});
#4
0
in your original post, you were passing the value to the element with ID shorePipe, and your input had ID avgSubPipe
在原始帖子中,您将值传递给ID为shorePipe的元素,并且您的输入具有ID avgSubPipe
element ID's have to match. ANd, you have to fire the function
元素ID必须匹配。你需要解雇这个功能
the HTML
<tr>
<td>Quantity to be dredged</td>
<td><input type="number" class="" id="avgSubPipe" readonly ></td>
</tr>
and the Javascript:
和Javascript:
var shorePipe = function() {
var val = 100;
var val2 = 100;
var total = val * val2 / 100;
// return total;
document.getElementById('shorePipe').value = total;
};
// calling function
shorePipe();
#1
2
Since your code works perfectly fine (beside </input>
), the only issue I see here is that you're probably not calling shorePipe();
once the DOM is read and ready.
由于你的代码完全正常(在 旁边),我在这里看到的唯一问题是你可能没有调用shorePipe();一旦DOM被读取并准备就绪。
So put your JS right before the closing </body>
tag
所以把你的JS放在结束 标签之前
<script>
var shorePipe = function() {
var val = 100;
var val2 = 100;
var total = val * val2 / 100;
// return total;
document.getElementById('shorePipe').value = total;
};
shorePipe();
</script>
</body>
notice also that </input>
is invalid. <input>
is in the group of void elements and should not have a closing tag.
另请注意 无效。 在void元素组中,不应该有结束标记。
#2
0
You have to call the function
你必须调用该函数
shorePipe();
Here's a fiddle: https://jsfiddle.net/2vxdyvgz/
这是一个小提琴:https://jsfiddle.net/2vxdyvgz/
#3
0
- Input doesn't need to close itself.
- Make sure you actually call your JS function somewhere.
输入不需要自行关闭。
确保你实际上在某处调用了JS函数。
I created this JSFiddle for you: https://jsfiddle.net/pd12edbp/2/
我为你创建了这个JSFiddle:https://jsfiddle.net/pd12edbp/2/
<input type="number" class="" id="shorePipe" readyonly>
JS:
var shorePipe = function() {
var val = 100;
var val2 = 100;
var total = val * val2 / 100;
// return total;
document.getElementById('shorePipe').value = total;
};
$(document).ready(function(){
shorePipe();
});
#4
0
in your original post, you were passing the value to the element with ID shorePipe, and your input had ID avgSubPipe
在原始帖子中,您将值传递给ID为shorePipe的元素,并且您的输入具有ID avgSubPipe
element ID's have to match. ANd, you have to fire the function
元素ID必须匹配。你需要解雇这个功能
the HTML
<tr>
<td>Quantity to be dredged</td>
<td><input type="number" class="" id="avgSubPipe" readonly ></td>
</tr>
and the Javascript:
和Javascript:
var shorePipe = function() {
var val = 100;
var val2 = 100;
var total = val * val2 / 100;
// return total;
document.getElementById('shorePipe').value = total;
};
// calling function
shorePipe();