单击按钮javaScript后更改td单元格的值

Changing the value of a td cell after clicking a button javaScript

本文关键字:td 单元格 按钮 javaScript 单击      更新时间:2023-09-26

请注意,一些HTML已被省略。单击标记为undo的按钮后,我正试图将id为"name"的td单元格中的值更改为0。我想不通。我需要更改它,以便用户看到0。谢谢

<!DOCTYPE html>
<html>
  <script src="toUndo.js"  type="text/javascript"></script>
  <script src="undo.js" type="text/javascript"></script>
  <body>
  </tr>
  </thead>
  <tbody>
    <tr class="results" id="first">
  <td>First Place</td>
      <td><input type="number" id="name"/></td>
    </tr>
    <button id="undo">undo</button>  
    <script type="text/javascript">undoName();</script>
</body>
</html>

java脚本文件:toUndo.js

function clearIt {
document.getElementById("name").value = 0;
}

java脚本文件:undo.js

function undoName {
undoButton = document.getElementById("undo");
undoButton.onclick = clearIt;    

}

可以这样做。。。

<!DOCTYPE html>
<html>
  <script type="text/javascript">
function clearIt() {
document.getElementById("name").value = 0;
}
</script>
  <body >
  <table>
    <tr class="results" id="first">
  <td>First Place</td>
      <td><input type="number" id="name"/></td>
    </tr>
</table>
    <button id="undo" onclick='clearIt()'>undo</button>  
</body>
</html>
您所犯的错误是,函数声明后缺少括号,它必须是function undoName(){...clearIt(){...,而不是function undoName{...clearIt{...

Javascript代码中存在一些语法错误。请参阅所附的代码片段。

function clearIt() {
    console.log(document.getElementById("name"));
	document.getElementById("name").value = '0';
}
function undoName() {
	undoButton = document.getElementById("undo");
	undoButton.onclick = clearIt;
}
undoName();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
 <table>
  <tbody>
    <tr class="results" id="first">
      <td>First Place</td>
      <td><input type="number" id="name"/></td>
    </tr>
  </tbody>
 </table>
 <button id="undo">undo</button>
</body>