【问题标题】:Clear all fields on page with submit [duplicate]使用提交清除页面上的所有字段[重复]
【发布时间】:2017-03-26 14:21:37
【问题描述】:

My code

我正在尝试在提交后清除字段。我已经尝试了我在谷歌搜索上看到的所有内容,但没有任何效果

<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 = "";
  }
}
`}

【问题讨论】:

    标签: javascript


    【解决方案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
    }
    

    【讨论】:

    【解决方案2】:

    这个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 = "";
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 2021-01-25
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多