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

时间:2014-10-02 00:21:38

标签: javascript html

请注意,某些HTML已被省略。我试图用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;    

}

2 个答案:

答案 0 :(得分:2)

可以这样做......

&#13;
&#13;
<!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>
&#13;
&#13;
&#13; 您所犯的错误是,在函数声明后缺少括号,它必须是function undoName(){...clearIt(){...不是function undoName{...clearIt{...

答案 1 :(得分:1)

您在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>