微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果字段由jQuery清空,则禁用发送按钮

如果有一个或多个输入字段为空,如何禁用发送按钮?

我在伪代码中的尝试

if ( $("input:empty") ) {
    $("input:disabled") 
}
else 
  // enable the ask_question -button

我一直在阅读这些文章而没有找到正确的解决方

> Official docs about empty:这就像是不相关的,因为它会查找所有输入字段
> a thread about disabled -feature in jQuery:这似乎是相关的
> to be able to test jQuery Firefox/terminal:获得测试环境对我最有帮助

我现在使用以下代码.
它包含一个关于角色的错误;但是我找不到它.

#1

$(document).ready(function(){
    $("#ask_form").validate(){
        rules: {
            username {
                required: true,minlenghth: 2
            },email: {
                required: true;
                minlength: 6
            },password {
                required: true,minlength: 6
            }
        } 
    });
}

#2 Meder的代码

我轻轻地修改了Meder的代码.它会永久禁用send -button,因此它也有bug.

$(document).ready(function(){
    var inputs = $('input','#ask_form'),empty = false;
    // can also use :input but it will grab textarea/select elements and you need to check for those..

    inputs.each(function() {
        if ( $(this).val() == '' ) {
            empty = true;
            return;
        }
    });

    if ( empty ) {
        $('.ask_question').attr('disabled','disabled'); // or true I believe.
    }
});

解决方法

var $submit = $("input[type=submit]");
if ( $("input:empty").length > 0 ) {
   $submit.attr("disabled","disabled");
} else {
   $submit.removeAttr("disabled");
}

原文地址:https://www.jb51.cc/jquery/177475.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐