为什么在我单击JavaScript中的按钮后会显示该值然后消失? [英] Why does the value is displayed and then disappear after I click on the button in JavaScript?

查看:78
本文介绍了为什么在我单击JavaScript中的按钮后会显示该值然后消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我点击按钮时,我会在文本字段结果中获取输出一秒钟,然后它就消失了......为什么它会消失? (我试图将功能放在体内..它没有帮助..)

Whenever I click on the button I get the output for a second in the text-field "Result" and then it's gone.. why does it disappear? (I have tried to place the function in the body.. it doesn't help..)

<html>
    <head>
        <script>
            function someFunction()
            {
                with (document.formy)
                {
                    resultsBox.value= "In " + yearsBox.value + "you will be" + ageBox.value + yearsBox.value +" years old.";
                }
            }
        </script>
    </head>

    <body>
        <form name="formy">
            Enter your current age: <input id="ageBox" type="text" value="18" />
            <br/>
            Enter number of years: <input id="yearsBox" type="text" value="5" />
            <br/>
            Click this button: <button onclick="someFunction()">Click me!   </button>
            <br/>
            Results: <input id="resultsBox" type="text" />
        </form>
    </body>
</html>


推荐答案

按钮的默认类型是提交。单击按钮时,表单将提交并重新加载页面。您可以通过在按钮上设置type属性来避免这种情况:

The default type for buttons is submit. When you click the button the form is submitting and reloading the page. You can avoid this by setting the type attribute on the button:

<button type="button" onclick="someFunction()">Click me!</button>

这篇关于为什么在我单击JavaScript中的按钮后会显示该值然后消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆