gpt4 book ai didi

javascript - 为什么我在JavaScript中点击按钮后显示值然后消失?

转载 作者:行者123 更新时间:2023-11-30 08:03:58 25 4
gpt4 key购买 nike

每当我点击按钮时,我都会在文本字段“Result”中得到一秒钟的输出,然后它就消失了。为什么它会消失? (我试图将函数放在正文中..它没有帮助..)

<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>

最佳答案

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

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

关于javascript - 为什么我在JavaScript中点击按钮后显示值然后消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21263780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com