【问题标题】:remove disabled attribute from button bootstrap 4从按钮引导程序 4 中删除禁用属性
【发布时间】:2021-01-13 20:54:31
【问题描述】:

我正在使用 jquery 3.3.1 验证我的表单中的电子邮件,但是当我尝试从表单提交按钮中删除 disabled 属性时,它没有做任何事情......我的意思是,如果响应是,它只会禁用按钮真的..但是当响应为假时,它不会删除该属性。它也没有将css颜色更改为绿色......密码匹配器工作正常,但电子邮件却没有任何地方可悲。有没有机会帮我看看这里有什么问题?

这是代码

    $(document).ready(function () {
    $('#registerform').validate();
    $('#password, #confirmpassword').on('keyup', function () {
        if ($('#password').val() == $('#confirmpassword').val()) {
            $('#alertPassword').html('<li><i class="fa fa-check text-success"></i>&nbsp; Password matching</li>').css('color', 'green');
            $("#submitsn").attr("disabled", false);
        } else {
            $('#alertPassword').html('<li><i class="fa fa-times text-success"></i>&nbsp; Password not matching</li>').css('color', 'red');
            $("#submitsn").attr("disabled", true);
        }
    });
    $("#email").live("blur", function (e) {
        $("#alertBadgeemail").hide();
        if ($("#email").val() == null || $("#email").val() == "") {
            $("#alertBadgeemail").show();
            $("#submitsn").attr("disabled", false);
            $("#alertBadgeemail").html("Email is required field.").css("color", "red");
        } else {
            $.ajax({
                type: "POST",
                url: "check-email.sn",
                data: $('#registerform').serialize(),
                dataType: "html",
                cache: false,
                success: function (msg) {
                    $("#alertBadgeemail").show();
                    $("#alertBadgeemail").html(msg).css("color", "red");
                    $("#submitsn").attr('disabled', 'disabled');
                },
                error: function (msg) {
                    $("#alertBadgeemail").show();
                    $("#alertBadgeemail").html(msg).css("color", "green");
                    $("#submitsn").removeAttr('disabled', 'disabled');
                }
            });
        }
    });
});

【问题讨论】:

  • Disabled 是一个属性。试试$("#submitsn").prop("disabled", false);
  • .live 是从 jQuery 中删除的已弃用的事实,因为 1.9 根本不使用它 - 也请发布 minimal reproducible example

标签: jquery validation


【解决方案1】:

在 bootstrap4 中 disable 是一个类值,因此您必须将其从类属性中删除:

$("#submitsn").removeClass('disabled');

也许你添加了禁用属性,所以你也必须删除它:

$("#submitsn").removeAttr('disabled');

【讨论】:

    【解决方案2】:

    您没有正确使用 .removeAttr。它只有一个参数。请查看下一个链接:

    https://www.w3schools.com/jquery/html_removeattr.asp

    $("#submitsn").removeAttr('disabled');
    

    另外,我不确定链接是否适用于更改颜色。我宁愿做这样的事情:

    var emailBadge = $("#alertBadgeemail");
    emailBadge.show();
    emailBadge.html(msg); //check is msg string or object
    emailBadge.css("color", "green");
    

    一般建议是尽量避免重复选择器,这可能会影响浏览器性能。

    【讨论】:

    • 这是一种黑客解决方案,因为 disabled 是 property, not a attribute。此外,链接比单独创建变量和调用方法更有效,因此您回答的第二部分并不是真正的最佳实践。
    猜你喜欢
    • 2020-10-14
    • 1970-01-01
    • 2022-09-28
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    相关资源
    最近更新 更多