简体   繁体   中英

Stop refresh on dynamically loading html contents on a button click

I have a HTML page, in which I'm dynamically loading contents on Button Click using javascript. This loads my entire page as a result previous form entries gets lost. How to avoid this? How to retain values and stop refreshing?

You probably need to return false in the javascript function handler. You might want to look into event.preventDefault as well.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM