迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

JavaScript 清除输入

作者:迹忆客 最近更新:2024/03/20 浏览次数:

在 JavaScript 中,我们通常会考虑一个特定的属性来处理输入值。value 属性与 getElementById() 方法一起获取输入字段的详细信息。

稍后,我们可以根据条目执行一些条件。同样,我们也可以依赖一些 HTML 属性来连接输入元素和 JavaScript 函数。

在这里,我们将展示一些可以通过多种方式实现的示例。


在 JavaScript 中使用 onfocus 属性清除输入字段

我们将首先为该示例声明一个具有定义值的输入字段。此外,一旦光标指向或聚焦在输入框中,输入元素将跳转以执行函数 clearF(this)

如果满足函数的条件,它将删除该值,从而清除输入。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>test</title>
</head>
<body>
<input type="text" value= "abc@gmail.com" onfocus="clearF(this)">
<script>
    function clearF(target) {
        if (target.value == 'abc@gmail.com') {
            target.value = "";
        }
    }
</script>
</body>
</html>

输出:

当光标放置并单击输入字段时,其内容消失。this 指的是它对特定元素的值。


在 JavaScript 中使用条件语句清除表单输入值

根据一个表单提交,需要有一个对应的按钮元素来存储数据。我们将采用一个 form 元素,与之关联的 button 将带有一个 onclick 属性来触发一个函数。

该函数将检查输入字段是否为空。如果它找到任何内容,它将简单地将其重新初始化为空。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>test</title>
</head>
<body>
  <form>
<input type="text" id="name">
    <input type="button" value="clear" onclick="clearF()">
 </form>
<script>
    function clearF() {
      var grab = document.getElementById("name");
        if (grab.value !="") {
            grab.value = "";
        }
    }
</script>
</body>
</html>

输出:

在这里,我们有一个将采用 idgrab 表单实例。单击按钮时,该函数将检查输入是否为空,并按照条件语句描述的方式执行。


使用 reset() 方法来清除 JavaScript 中的表单输入字段

我们还将检查表单以通过 reset() 方法清除其输入。它所需要的只是创建表单的一个实例,然后单击按钮,它将重置整个表单,使插入的内容消失。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>test</title>
</head>
<body>
  <form id="Form">
  Name: <input type="text"><br><br>
  <input type="button" onclick="myFunction()" value="Reset form">
</form>

<script>
function myFunction() {
    document.getElementById("Form").reset();
}
</script>
</body>
</html>

输出:

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便