【发布时间】:2018-06-03 14:33:12
【问题描述】:
如果输入到文本框中的文本与预定义的文本不匹配,我需要重置 html 表单。但是,如果我使用 reset() 函数,即使文本与预定义文本匹配,表单也会被重置。我该如何避免这个问题?这是我的代码。
function showData() {
var code= document.getElementById("bcno").value.trim();
switch(code)
{
case "WASTE1":
document.getElementById('display').innerHTML ="This Is Waste Type 1.";
break;
case "WASTE2":
document.getElementById('display').innerHTML ="This Is Waste Type 2.";
break;
case "WASTE3":
document.getElementById('display').innerHTML ="This Is Waste Type 3.";
break;
case "WASTE4":
document.getElementById('display').innerHTML ="This Is Waste Type 4.";
break;
default:
setTimeout(function(){document.getElementById("updateform").reset();}, 2000);
document.getElementById('display').innerHTML ="The text does not match. Form will be reset momentarily!!.";
break;
}
这是 html 部分。
<form action="dbupdate.php" method="post" id="updateform"
name="updateform">
<label for="bcno">Item ID: </label>
<input type="text" autofocus="autofocus" name="bcno" id="bcno"
oninput="showData()" autocomplete="off" /> <br /><br />
<div id="display"></div>
<label for="ino">Quantity: </label>
<input type="number" id="ino" name="ino" value="1" /><br /><br />
<input type="button" onclick="document.updateform.submit();"
value="Confirm"> </form>
bcno 是文本框的 id。我究竟做错了什么? 我也在使用 Google Chrome(版本 63.0.3239.132,最新)来测试我的代码
【问题讨论】:
-
发送消息“这是 X 型废物”。输入正确的代码时显示?
-
是的。他们留下来。但表格被重置。我不能这样做,因为我必须将数据转发到另一个页面。
-
你在哪里使用这个功能?你在提交表单时使用它吗?请分享您的
HTML代码。 -
<form action="dbupdate.php" method="post" id="updateform" name="updateform"> <label for="bcno">Item ID: </label> <input type="text" autofocus="autofocus" name="bcno" id="bcno" oninput="showData()" autocomplete="off" /> <br /><br /> <div id="display"></div> <label for="ino">Quantity: </label><input type="number" id="ino" name="ino" value="1" /><br /><br /> <input type="button" onclick="document.updateform.submit();" value="Confirm"> </form> -
如果你不能从评论中得到它,我也更新了这个问题。
标签: javascript function debugging reset