Javascript 文本框内容在更改后立即重置
我已经建立了一个基本的脚本来做一个基础数学,点击加1,点击减1 问题是,每次单击该按钮时,您都可以看到文本框内容增加1或减少1,但在四分之一秒内,表单会重置并再次显示初始值0Javascript 文本框内容在更改后立即重置,javascript,html,Javascript,Html,我已经建立了一个基本的脚本来做一个基础数学,点击加1,点击减1 问题是,每次单击该按钮时,您都可以看到文本框内容增加1或减少1,但在四分之一秒内,表单会重置并再次显示初始值0 <html> <head> <script> function inc() { document.content.quant.value++; } function dec() { document.content.quant.value--; } </script> &l
<html>
<head>
<script>
function inc()
{
document.content.quant.value++;
}
function dec()
{
document.content.quant.value--;
}
</script>
</head>
<body>
<form name="content">
<input type="text" id="quant" value="0">
<button onclick="inc()">increase</button>
<button onclick="dec()">decrease</button>
</form>
</body>
</html>
功能公司
{
document.content.quant.value++;
}
函数dec()
{
document.content.quant.value--;
}
增加
减少
我肯定我犯了一些愚蠢的错误-有什么想法吗?在按钮上添加type=“button”
<button type="button" onclick="inc()">increase</button>
增加
没有类型
属性的按钮的默认行为是提交它所属的表单。如果您将类型
更改为按钮
它将不会做任何事情:)将type=“button”
添加到按钮中
<button type="button" onclick="inc()">increase</button>
增加
没有
类型
属性的按钮的默认行为是提交它所属的表单。如果您将类型
更改为按钮
它将不会做任何事情:)当您单击按钮时,表单将被提交。使用preventDefault可防止此情况
<html>
<head>
<script>
function inc()
{
document.content.quant.value++;
event.preventDefault();
}
function dec()
{
document.content.quant.value--;
event.preventDefault();
}
</script>
</head>
<body>
<form name="content">
<input type="text" id="quant" value="0">
<button onclick="inc()">increase</button>
<button onclick="dec()">decrease</button>
</form>
</body>
</html>
功能公司
{
document.content.quant.value++;
event.preventDefault();
}
函数dec()
{
document.content.quant.value--;
event.preventDefault();
}
增加
减少
当您单击按钮时,表单将被提交。使用preventDefault可防止此情况
<html>
<head>
<script>
function inc()
{
document.content.quant.value++;
event.preventDefault();
}
function dec()
{
document.content.quant.value--;
event.preventDefault();
}
</script>
</head>
<body>
<form name="content">
<input type="text" id="quant" value="0">
<button onclick="inc()">increase</button>
<button onclick="dec()">decrease</button>
</form>
</body>
</html>
功能公司
{
document.content.quant.value++;
event.preventDefault();
}
函数dec()
{
document.content.quant.value--;
event.preventDefault();
}
增加
减少