3 回答

TA贡献1818条经验 获得超3个赞
var fruits = document.getElementById('fruits');
function calculate(event) {
//use event here to get the apples element value on every change event.target.value so you dont have to fetch the element every time
fruits.value = (parseInt(event.target.value) + 5);
//parse it as nummber and + 5
}
var inputElement = document.getElementById('apples');
inputElement.addEventListener('change', calculate);
<table class="egt">
<tr>
<th>apples</th>
<th>fruits</th>
</tr>
<tr>
<td>
<input type="number" id="apples">
</td>
<td>
<input type="number" id="fruits">
</td>
</tr>
</table>

TA贡献1752条经验 获得超4个赞
value输入值在标签的属性上更新input。所以需要得到像fruits.value和这样的价值apple.value。
并且apples.value是字符串,所以求和运算需要转为数字。
var apples = document.getElementById('apples');
var fruits = document.getElementById('fruits');
function calculate() {
fruits.value = Number(apples.value) + 5;
}
// Get all the input columns
var inputElement = document.getElementById('apples');
// Add Event Listeners to all the input columns
inputElement.addEventListener('change', calculate);
<table class="egt">
<tr>
<th>apples</th>
<th>fruits</th>
</tr>
<tr>
<td>
<input type="number" id="apples">
</td>
<td>
<input type="number" id="fruits">
</td>
</tr>
</table>

TA贡献1836条经验 获得超13个赞
从中获取字符串值apples.value并用于parseInt转换为整数,然后更新水果。
var apples = document.getElementById('apples');
var fruits = document.getElementById('fruits');
function calculate() {
fruits.value = parseInt(apples.value) + 5;
}
// Get all the input columns
var inputElement = document.getElementById('apples');
// Add Event Listeners to all the input columns
inputElement.addEventListener('change', calculate);
<table class="egt">
<tr>
<th>apples</th>
<th>fruits</th>
</tr>
<tr>
<td>
<input type="number" id="apples">
</td>
<td>
<input type="number" id="fruits">
</td>
</tr>
</table>
添加回答
举报