简体   繁体   中英

when i press enter key, checkbox displayed in <div> disappears

This is the code which I tried, it should work like this: Text written in text-box, should appear as check-box in div tag.

I used onchange() function. It worked fine when I moved out of text box, but when I pressed Enter key, check-box appears for few seconds and disappears. Nothing is displayed in div.

How to solve this problem? What really happens when I press Enter while writing in text-box?

<html>
    <body>
        <form id="form1" name="form1" method="post" action="">
            <p>
                <label for="textfield">Hello</label>
                <input type="text" name="textfield" id="textfield" onChange="add_items(this.value)" />
            </p>
            <br>
            <div id="item"></div>
        </form>

        <script>
        k=""
        function add_items(m)
        {
            k=k+"<input type=checkbox name=menu[] id=menu[] value="+m+">"+m+"&nbsp;";
            document.getElementById("item").innerHTML=k
        }
        </script>
    </body>
</html>

You are submitting the form by pressing the Enter key.

Add onsubmit="return false;" to your form tag.

onsubmit="return false;" will solve your issue

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