要禁用,如果发送按钮领域的jQuery空 [英] To disable a send button if fields empty by jQuery

查看:118
本文介绍了要禁用,如果发送按钮领域的jQuery空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何禁用发送 - 按钮,如果有一个或多个输入-fields空的?

我在伪code尝试

 如果($(输入:空)){
    $(输入:已禁用)
}
其他
  //启用ask_question -button

我一直在阅读这些文章没有找到一个合适的解决方案。


  1. 有关空的官方文档:这就像不相关的,因为它看起来所有输入-fields

  2. a关于jQuery的残疾人线程-feature:这似乎是相关的

  3. 到能够测试的jQuery火狐/终端:在向得到一个测试环境会帮我最

我用的那一刻以下code。
它包含字符一个错误。,但我不能找到它。

1

  $(文件)。就绪(函数(){
    $(#ask_form)。验证(){
        规则:{
            用户名 {
                要求:真实,
                minlenghth:2
            },
            电子邮件:{
                要求:真实的;
                MINLENGTH个:6
            },
            密码{
                要求:真实,
                MINLENGTH个:6
            }
        }
    });
}

#2梅德的code

我slighhtly修改梅德的code。它会禁用发送 -button永久所以它有一个bug了。

  $(文件)。就绪(函数(){
    VAR输入= $('输入','#ask_form'),空= FALSE;
    //还可以使用:输入,但它会抢的textarea / select元素,你需要检查这些..    inputs.each(函数(){
        如果($(本).VAL()==''){
            空=真;
            返回;
        }
    });    如果(空){
        $('。ask_question')ATTR(禁用,禁用)。 //或真实的,我相信。
    }
});


解决方案

 变量$提交= $(输入[类型=提交]);
如果($(输入:空。)长度大于0){
   $ submit.attr(禁用,已禁用);
}其他{
   $ submit.removeAttr(已禁用);
}

How can you disable the send -button if there is one or more input -fields empty?

My attempt in pseudo-code

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

I have been reading these articles without finding a right solution

  1. Official docs about empty: this is like not relevant because it looks for all input -fields
  2. a thread about disabled -feature in jQuery: this seems to be relevant
  3. to be able to test jQuery Firefox/terminal: to get a testing environment would help me most

I use at the moment the following code. It contains one bug about the character ;, but I cannot find it.

#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's code

I slighhtly modified Meder's code. It disables the send -button permanently so it has a bug too.

$(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");
}

这篇关于要禁用,如果发送按钮领域的jQuery空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆