javascript 中的函数仅适用于其中一行

我有这个有 2 列和 2 行的小表。我在“Fuel”中手动输入一个数字,应该在“Double_Fuel”列上显示双倍数。不幸的是,它只将我的功能应用于第一行。在第二行它不起作用。有人知道为什么吗?提前致谢

http://img4.mukewang.com/645c86c30001938204120107.jpg

<html>


<body>


<style> table, th, td {border: 1px solid black;} </style>


<! --COMIENZO TABLA -->


<table id="MyTable" class="egt">



<! --PRIMERA LINEA -->

 

<tr>    

            <th>Fuel</th>

            <th>Double_Fuel</th>

</tr>

  

<! --SEGUNDA LINEA -->   


<tr> 

     <td>

     <input type="number" id="fuel"> 

     </td>  

    

     <td>

     <input type="number" ID="fuel2" >

     </td>   

</tr>


<! --TERCERA LINEA -->  


<tr> 

     <td>

     <input type="number" id="fuel"> 

     </td>  

    

     <td>

     <input type="number" ID="fuel2" >

     </td>   

</tr>




</table>



<script>

var fuel = document.getElementById('fuel');

var fuel2 = document.getElementById('fuel2');

function calculate(){

fuel2.value = Number(fuel.value)*2;

}

var inputElement = document.getElementById('fuel');

inputElement.addEventListener('change', calculate);

</script>



</body>

</html>


阿波罗的战车
浏览 193回答 3
3回答

神不在的星期二

ids 在页面上应该是唯一的,因此getElementById只返回一个元素(如果没有找到匹配的 id,则返回 null),但是您可以在数据集属性的帮助下并使用类而不是 ids 来执行计算函数,如下所示:function calculate() {&nbsp; const result = document.querySelector(`#${this.dataset.result}`);&nbsp; result.value = Number(this.value) * 2;}const inputElements = document.querySelectorAll('.fuel');inputElements.forEach(input => input.addEventListener('change', calculate));table,th,td {&nbsp; border: 1px solid black;}<! --COMIENZO TABLA --><table id="MyTable" class="egt">&nbsp; <! --PRIMERA LINEA -->&nbsp; <tr>&nbsp; &nbsp; <th>Fuel</th>&nbsp; &nbsp; <th>Double_Fuel</th>&nbsp; </tr>&nbsp; <! --SEGUNDA LINEA -->&nbsp; <tr>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; <input type="number" class="fuel" data-result="fuel1">&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; <input type="number" id="fuel1">&nbsp; &nbsp; </td>&nbsp; </tr>&nbsp; <! --TERCERA LINEA -->&nbsp; <tr>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; <input type="number" class="fuel" data-result="fuel2">&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; <input type="number" id="fuel2">&nbsp; &nbsp; </td>&nbsp; </tr></table>

狐的传说

发生这种情况是因为您复制了IDs,这在单个网页上必须是唯一的。因此,我更改了IDstoclasses并使用了该.querySelectorAll()方法,然后通过forEach循环将该方法添加到您可能拥有的.addEventListener()所有可能(以供将来支持)。<input><table>这是工作代码:<html><body><style> table, th, td {border: 1px solid black;} </style><! --COMIENZO TABLA --><table id="MyTable" class="egt"><! --PRIMERA LINEA -->&nbsp;<tr>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Fuel</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Double_Fuel</th></tr>&nbsp;&nbsp;<! --SEGUNDA LINEA -->&nbsp; &nbsp;<tr>&nbsp;&nbsp; &nbsp; &nbsp;<td>&nbsp; &nbsp; &nbsp;<input type="number" class="fuel">&nbsp;&nbsp; &nbsp; &nbsp;</td>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;<td>&nbsp; &nbsp; &nbsp;<input type="number" class="fuel2" >&nbsp; &nbsp; &nbsp;</td>&nbsp; &nbsp;</tr><! --TERCERA LINEA -->&nbsp;&nbsp;<tr>&nbsp;&nbsp; &nbsp; &nbsp;<td>&nbsp; &nbsp; &nbsp;<input type="number" class="fuel">&nbsp;&nbsp; &nbsp; &nbsp;</td>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;<td>&nbsp; &nbsp; &nbsp;<input type="number" class="fuel2" >&nbsp; &nbsp; &nbsp;</td>&nbsp; &nbsp;</tr></table><script>&nbsp;&nbsp;&nbsp; var fuels = document.querySelectorAll('.fuel');&nbsp;&nbsp;&nbsp; fuels.forEach(function(elem) {&nbsp; &nbsp; elem.addEventListener("change", function() {&nbsp;this.parentElement.nextElementSibling.firstElementChild.value = Number(this.value)*2;&nbsp; &nbsp; });&nbsp; });&nbsp;&nbsp;</script></body></html>

慕莱坞森

你可以这样做:var fuel = document.getElementById('fuel');var fuel2 = document.getElementById('fuel2');function calculate(elem) {&nbsp; document.getElementById('double-' + elem.target.id).value = Number(elem.target.value) * 2;}document.getElementById('fuel').addEventListener('change', calculate);document.getElementById('fuel2').addEventListener('change', calculate);table,th,td {&nbsp; border: 1px solid black;}<table id="MyTable" class="egt">&nbsp; <tr>&nbsp; &nbsp; <th>Fuel</th>&nbsp; &nbsp; <th>Double_Fuel</th>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; <input type="number" id="fuel">&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; <input type="number" ID="double-fuel">&nbsp; &nbsp; </td>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; <input type="number" id="fuel2">&nbsp; &nbsp; </td>&nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; <input type="number" ID="double-fuel2">&nbsp; &nbsp; </td>&nbsp; </tr></table>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript