3 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
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貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊
value輸入值在標(biāo)簽的屬性上更新input。所以需要得到像fruits.value和這樣的價(jià)值apple.value。
并且apples.value是字符串,所以求和運(yùn)算需要轉(zhuǎn)為數(shù)字。
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貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊
從中獲取字符串值apples.value并用于parseInt轉(zhuǎn)換為整數(shù),然后更新水果。
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>
添加回答
舉報(bào)