Javascript AppendChild 自动刷新表单

Javascript AppendChild autorefreshes the form

本文关键字:刷新 表单 AppendChild Javascript      更新时间:2023-09-26

我有一个小代码,可以将列表项从一个列表移动到另一个列表。 该代码在IE8中工作,但不能在IE10或chrome中工作。 onclick 事件后,列表项从左列表移动到右列表,但随后,页面似乎会自动刷新。

如果我删除标签,那么代码就可以了,但我需要标签。 如何停止 javascript 以在发生 onclick 事件时不刷新页面。 我尝试了其他帖子中提到的一些解决方案,但没有一个奏效。 如果您能在这方面帮助我,请不胜感激。 我不想发布所有代码。因此,我为此创建了一个示例页面。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta content="text/html; charset=ISO-8859-1"
 http-equiv="content-type">
  <title>Test</title>
  <script>
function AppendList()
{
//event.preventDefault(); I also tried AppendList(event)
var node = document.getElementById("batchReportLists0").lastChild;
document.getElementById("batchReportLists1").appendChild(node);
alert("Added Last Item");
//return false;
//window.onbeforeunload = function(){
//return "Refresh";
}
  </script>
</head>
<body>
<form name="batchReportForm" enctype="multipart/form-data">
  <table style="text-align: left; width: 100%;" border="1"
 cellpadding="2" cellspacing="2">
    <tbody>
      <tr>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td>
        <select multiple="multiple" size="15" name="T1"
 class="bodyText" id="batchReportLists0">
        <option>Msg1</option>
        <option>Msg2</option>
        </select>
        </td>
        <td> <button onclick="AppendList();">
&gt;&gt;&gt; </button>
        </td>
        <td>
        <select multiple="multiple" size="15" name="T2"
 class="bodyText" id="batchReportLists1">
        <option>Msg3</option>
        <option>Msg4</option>
        </select>
        </td>
      </tr>
    </tbody>
  </table>
  <br>
</form>
</body>
</html>

让它成为

<button type="button">Button</button>

默认情况下,<button>充当提交。