如何在生成警报后清除HTML输入标记

时间:2016-04-27 08:18:16

标签: javascript html

当输入的数字不是整数时,我使用下面的函数生成警报。但我还需要清除HTML表单。我使用了id.value= "";但是没有用。欢迎提出任何建议:



function myFunction(id) {
  var x = parseInt(document.getElementById(id).value);
  if (!(x === parseInt(x, 10))) {
    alert("Empployee ID can be integer numbers only.");
    id.value = "";
  } else {

  }
}

<div class="header1">
  <input type="text" id="ename" onblur="myFunction('ename')" name="name" placeholder="Enter Employee ID" required="" />
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

您的id.value不是HTML元素。您正尝试访问id变量的value属性...类似这样"ename".value

试试这个:

document.getElementById(id).value = 'VALUE';

function myFunction(id) {
  
  var x = parseInt(document.getElementById(id).value);
  
  if (!(x === parseInt(x, 10))) {
  
    alert("Empployee ID can be integer numbers only.");
    document.getElementById(id).value = "";
  } 
  else {

  }
}
<div class="header1">
  <input type="text" id="ename" onblur="myFunction('ename')" name="name"  placeholder="Enter Employee ID" required="" />
</div>

答案 1 :(得分:0)

您正在users = result;的value属性中设置空字符串。 idid。因此,如果id属性等于函数参数,则需要在文档中的某处放置一个空字符串。

你需要使用:

string

答案 2 :(得分:0)

您无法使用id.value 因为你函数传递的id是一个字符串。 既然您已经通过id,那么您可以使用:

document.getElementById(id).value = "";

答案解释