我想在提交后清除字段。我尝试过在谷歌搜索中看到的所有内容都没有工作
<input type="text" id="a1">
<input type="button" class="button" value="Submit" onclick="w001()">
<span id="a21"></span>
</form>
function w001() {
var a = document.getElementById("1");
if ((a.value == "a 7") || (a.value == "7")) {
document.getElementById('a21').innerHTML = 'correct';
} else {
document.getElementById('a21').innerHTML = 'incorrect';
}
`function w001Field() {
if (document.getElementById) {
document.a1.value = "";
}
}
`}
答案 0 :(得分:1)
document.a1.value = "";
无效。
使用document.getElementById("a21").value = ''
或
只是a.value = ''
,因为您已在var a = document.getElementById("a21");
最终代码
function w001() {
var a = document.getElementById("a21");
if ((a.value == "a 7") || (a.value == "7")) {
document.getElementById('answera21').innerHTML = 'correct';
} else {
document.getElementById('answera21').innerHTML = 'incorrect';
}
a.value = '' // reset value of input with id a21
}
答案 1 :(得分:0)
这个id:
var a = document.getElementById("1");
不存在
应该是
var a = document.getElementById("a1");
这个功能;
w001Field()
永远不会使用
将代码更改为
<form onsubmit="return false">
<input type="text" id="a1">
<input type="button" class="button" value="Submit" onclick="w001()">
<span id="a21"></span>
</form>
function w001() {
var a = document.getElementById("a1");
document.getElementById('a21').innerHTML = ((a.value == "a 7") || (a.value == "7")) ? 'correct' : 'incorrect';
document.getElementById("a1").value = "";
}