迹忆客 专注技术分享

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

JavaScript 禁用按钮单击

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

在 JavaScript 中,disabled 属性仅适用于以 type="submit" 为根的按钮。否则,它不会像一个提交按钮那样起作用;相反,它将接受多个提交。

类似地,jQuery 属性 disabled 适用于具有 submit 类型的按钮。另外,表单提交方法在这方面非常重要。

在我们的示例集中,我们将看到 JavaScript disabled 属性和 jQuery disabled 属性的实现。


使用 JavaScript disabled 属性禁用按钮单击

通常,我们定义一个表单及其方法来确保提交的目的。无论如何,JavaScript 的属性 disabled 让我们可以让按钮处于非活动状态。

在这里,我们将启动一个表示 submit 类型的 input 字段。稍后获取 input 标签的实例,我们将触发 disabled 属性。

代码片段:

<body>
    <p>Click the button to submit data!</p>
    <p>
        <input type='submit' value='Submit' id='btClickMe'
            onclick='save();'>
    </p>
    <p id="msg"></p>
</body>
<script>
    function save() {
      var dis = document.getElementById('btClickMe').disabled = true;
        var msg = document.getElementById('msg');
        msg.innerHTML = 'Data submitted and the button disabled ☺';
    }
</script>
</html>

输出:


使用 jQuery disabled 属性禁用按钮单击

在 jQuery 中,我们将有一个类似的 disabled 属性用例。这里的区别是我们将在 form 标签内启动 input 字段(submit 类型按钮)。

我们将确保这是否也适用于表单。

代码片段:

<body>
<form id="fABC" action="#" method="POST">
    <input type="submit" id="btnSubmit" value="Submit">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<input type="button" value="i am normal button" id="btnTest">
<script>
    $(document).ready(function () {
        $("#fABC").submit(function (e) {
            e.preventDefault();
            $("#btnSubmit").attr("disabled", true);
            $("#btnTest").attr("disabled", true);
            return true;
        });
    });
</script>

</body>
</html>

输出:

禁用按钮的 jQuery 方法类似于 JavaScript。在示例中,我们添加了一个带有 type=button 的通用按钮,该按钮无法由 disabled 属性或属性处理。

这就是 JavaScript 或 jQuery disabled 属性如何使提交按钮处于非活动状态。

转载请发邮件至 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便