如何清除错误信息?

时间:2016-03-08 21:04:30

标签: javascript

我试图显示错误消息"当用户输入字母而不是表格中的数字时,使用CSS的不同颜色,我做了。问题是当用户更正错误并写入数字时,错误消息仍然存在。我该如何解决这个问题?

<html>
  <head>
    <style>
    #error {color: red;}
    </style>
  </head>
  <body>
    <p id="error"></p>
    <p id="demo"></p>
    <input id="demo1" type="text" />
    <button onclick="vote()"> Click here</button>
    <script>
      function vote() {
        var age = document.getElementById("demo1").value;
        if (isNaN(age)) {
          document.getElementById("error").innerHTML = "Input Error!!";
        } else {
          document.getElementById("demo").innerHTML = (age < 18) ? "Too young to vote" : "Old enough to vote";
        }
      }
    </script>
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

在处理新条目之前添加import pandas as pd import numpy as np import matplotlib.pyplot as plt if plt.get_fignums(): plt.close('all') electric = pd.read_csv('electric.tsv', sep='\t') gas = pd.read_csv('gas.tsv', sep='\t') electric_df = pd.DataFrame(electric) gas_df = pd.DataFrame(ngma_nonheat) electric = electric_df['avg_daily']*30 gas = gas_df['avg_daily']*30 ## Create a plot for NGMA gas usage plt.figure("Usage Comparison") weights_electric = np.ones_like(electric)/float(len(electric)) weights_gas = np.ones_like(gas)/float(len(gas)) bins=np.linspace(0, 200, num=50) n, bins, rectangles = plt.hist(electric, bins, alpha=0.5, label='electric usage', normed=True, weights=weights_electric) plt.hist(gas, bins, alpha=0.5, label='gas usage', normed=True, weights=weights_gas) plt.legend(loc='upper right') plt.xlabel('Average 30 day use in therms') plt.ylabel('% of customers') plt.title('NGMA Customer Usage Comparison') plt.show() 进行清理:

document.getElementById("id").innerHTML = "";
function vote() {

  var aaa = document.getElementById("error");
  var bbb = document.getElementById("demo");
  
aaa.innerHTML = "";
bbb.innerHTML = "";  
  
  var age = document.getElementById("demo1").value;
  if (isNaN(age)) {
    aaa.innerHTML = "Input Error!!";
  } else {
    bbb.innerHTML = (age < 18) ? "Too young to vote" : "Old enough to vote";
  }
}
body {
  background: lavender;  
}

#error {
  color: red;
}

答案 1 :(得分:0)

您可以在输入上添加一个keyup事件,以便检查每个字符

<input id="demo1" type="text" onkeyup="validate()" />

function validate() {
    if (isNaN(age)) {
        document.getElementById("error").innerHTML = "Input Error!!";
    } else {
        document.getElementById("error").innerHTML = "";
    }
}