【问题标题】:disable all other inputs if command/specific input not filled in如果未填写命令/特定输入,则禁用所有其他输入
【发布时间】:2021-02-12 00:21:55
【问题描述】:

我正在尝试禁用特定表单“<form id="join">”中的所有其他输入 如果用户没有先填写 <input type="text" id="userkey" name="userkey" /> 输入,并且所有其他输入将保持禁用状态,直到填写“<form id="join">”输入。

这样做的原因是为了模拟用户行为,因此他们将首先加入我们的网站不和谐,原因有多种,我不会详细说明。我知道这不是一种安全的方法。所有用户数据都将在服务器端进行清理和验证,以保护站点/数据库。我知道可以绕过这个并仍然提交数据,用户数据将再次在服务器端进行清理和验证以保护站点/数据库。我之所以这样做,是因为即使在会员表格上写了一大笔笔记,他们仍然会尝试提交数据并绕过加入不和谐,从而难以与他们沟通。这是对网站进行白痴校对的一种尝试 - 它还阻止了大量垃圾邮件,因为垃圾邮件机器人通常无法加入不和谐。

这是一个非常简单的示例,我将从中推断出我们的实际会员表格。

这是htmt

<form id="join-membership">
    user key
    <br />
    <input type="text" id="userkey" name="userkey" />
    <br />
    <p>If you do not have a user key, please <a href="#">join our discord</a> to get one</p>
    Email
    <br />
    <input type="email" id="email" disabled="disabled" />
    <br />
    Username
    <br />
    <input type="text" id="username" name="username" disabled="disabled" />
    <br />
    Password
    <br />
    <input type="password" id="pass" name="password" disabled="disabled" />
    <br />
    Confirm Password
    <br />
    <input type="password" id="pass2" name="password2" disabled="disabled" />
    <br />
    About You
    <br />
    <textarea id="about" name="about" disabled="disabled"></textarea>
    <br />
    <input type="submit" id="submit" value="Register" disabled="disabled" />
</form>
<div id="test"></div>

    

这里是 JavaScript

<script>

(function() {
    $('form > input').keyup(function() {

        var email = true;
        var empty = false;
        $('form > input').each(function() {
            if ($(this).val() == '') {
                empty = true;
            }
        });

        if (email == empty) {
            $('#register').attr('disabled', 'disabled');
        } else {
            $('#register').removeAttr('disabled');
        }
    });
})()

</script>

请注意,我是 JavaScript 和网络的超级新手,并且研究并找到了禁用提交按钮的脚本,但是我们的表单有点复杂,我不希望用户尝试填写它来查找即使他们没有阅读说明,也无法提交。这是模型用户行为,旨在提供我希望的长期更好的用户体验。

感谢您提供的任何帮助。

【问题讨论】:

    标签: javascript html input disabled-input


    【解决方案1】:

    你好,你可以试试这样的

     (function() {
        $("#userkey").keyup(function() {
          const userkey = this.value;
            if (userkey) {
              $('.other-inputs input, .other-inputs textarea').removeAttr('disabled');
                } else {
                 $('.other-inputs input, .other-inputs textarea').attr('disabled', 'disabled');
             }
        });
    })()
    

    在用户键输入更改时,该功能正在检查userkey输入中是否存在值,如果存在它将从div other-input中的输入中删除disabled属性

    查看示例:https://jsfiddle.net/t4ub0xs3/

    【讨论】:

    • 您可以检查 userkey 一次并在元素集 $('.other-inputs input, .other-inputs textarea').removeattr('disable') 或 $ 上执行操作,而不是 foreach ('.other-inputs input, .other-inputs textarea').attr('disabled', 'disabled');
    • @Lety 修复了 thnx 的建议
    • 谢谢大家,我是有史以来最快乐的 12 岁!
    猜你喜欢
    • 1970-01-01
    • 2021-07-13
    • 2017-11-08
    • 1970-01-01
    • 2012-01-26
    • 2011-10-09
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多