单击按钮即可停止动态加载html内容的刷新

Stop refresh on dynamically loading html contents on a button click

本文关键字:html 刷新 加载 动态 按钮 单击      更新时间:2023-09-26

我有一个HTML页面,在其中我使用javascript动态加载Button Click上的内容。这会加载我的整个页面,导致以前的表单条目丢失。如何避免这种情况?如何保留值并停止刷新?

您可能需要在javascript函数处理程序中返回false。您可能还想查看event.preventDefault。

Below is my code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
    var counter=0;
    function addMore()
    {
        ++counter;
        var string = '<div><table border="1"><tr><td><label>Name</label><input type="text" name="txtName'+counter+'"/></td></tr><tr><td><label>Age</label><input type="text" name="txtAge'+counter+'"/></td></tr></table></div>';
document.getElementById("content").innerHTML+=string;
    }
</script>
</head>
<body>
<form action="display.php" method="post">
    <div id="content">
        <table>
            <tr>
                <td><label>Name</label><input type="text" id="txtName0" name="txtName0" />
                </td>
            </tr>
            <tr>
                <td>
                    <label>Age</label><input type="text" id="txtAge0" name="txtAge0" />
                </td>
            </tr>
            <tr>
                <td>
                    <input type="button" onclick="javascript:addMore()" />
                </td>
            </tr>
        </table>
    </div>
    <input type="submit" value="display" />`enter code here`
    </form>
</body>
</html>