Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 文本框内容在更改后立即重置_Javascript_Html - Fatal编程技术网

Javascript 文本框内容在更改后立即重置

Javascript 文本框内容在更改后立即重置,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

我已经建立了一个基本的脚本来做一个基础数学,点击加1,点击减1 问题是,每次单击该按钮时,您都可以看到文本框内容增加1或减少1,但在四分之一秒内,表单会重置并再次显示初始值0

<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();
}
增加
减少