简体   繁体   English

Jquery禁用按钮,直到两个输入具有最小长度

[英]Jquery disable a button until two inputs have minimum length

I am trying to keep an input disabled until 2 inputs have atleast 4 characters. 我试图保持输入禁用,直到2个输入至少有4个字符。 The closest I was able to find on SO was a user settling for 8 characters total but this would allow for one of the inputs to be blank in the other had over 8. Here is what I have tried: 我能够在SO上找到的最接近的用户是总共8个字符,但是这将允许其中一个输入为空白而另一个超过8.这是我尝试过的:

Without on() 没有on()

$('#ancestor input').keyup(function(){
  var foo = $('input[name=foo]').val();
  var bar = $('input[name=bar]').val();    
    if(foo.length > 4 && bar.length > 4){  
      $('#some-button').prop('disabled', false);
      }else{
      $('#some-button').prop('disabled', true);
      }  
});

With on() 随着on()

$('#ancestor input').on('keyup',function(){
  var foo = $('input[name=foo]').val();
  var bar = $('input[name=bar]').val();    
    if(foo.length > 4 && bar.length > 4){  
      $('#some-button').prop('disabled', false);
      }else{
      $('#some-button').prop('disabled', true);
      }  
});

Interestingly I can console.log(foo.length) and console.log(bar.length) and see both lengths ticking up just fine with each keyup . 有趣的是,我可以使用console.log(foo.length)console.log(bar.length)并且看到每个keyup两个长度都很好。

As A. Wolff has mentioned, your condition is using > instead of >= . 正如A. Wolff所提到的,你的条件是使用>而不是>=

if (foo.length >= 4 && bar.length >= 4) {  

If you have 2 input elements, you can also invert the condition: 如果您有2个输入元素,还可以反转条件:

$('#ancestor input').keyup(function(){
  var lengthA = $('input[name=foo]').val().length;
  var lengthB = $('input[name=bar]').val().length;

  $('#some-button').prop('disabled', lengthA < 4 || lengthB < 4);
});

Conceptually that means "disable if any input is not 4 characters long" instead of "do not disable if both inputs are more than 4 characters long". 从概念上讲,这意味着“如果任何输入不是4个字符,则禁用”,而不是“如果两个输入都超过4个字符,则禁用”。

Your code works as far as I can see. 你的代码尽我所能。 The only issue is that your condition checks for at least 5 characters, not 4 characters, due to the operator you've chosen to use. 唯一的问题是,由于您选择使用的运营商,您的条件将检查至少5个字符,而不是4个字符。

Use a >= instead, as this asserts something that is "equal to or greater than". 使用>=代替,因为这声明了“等于或大于”的东西。

 $('#ancestor input').on('keyup',function(){ var foo = $('input[name=foo]').val(); var bar = $('input[name=bar]').val(); $('#some-button').prop('disabled', !(foo.length >= 4 && bar.length >= 4)); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <form id="ancestor"> <input name="foo"> <input name="bar"> <input id="some-button" type="submit" disabled> </form> 

You could try matching the jquery object length against a filtered jquery object to see if they are different. 您可以尝试将jquery对象长度与已过滤的jquery对象进行匹配,以查看它们是否不同。

$('#ancestor input').on('input keyup change paste', function () {
    var els = $('input[name=foo],input[name=bar]');
    var filtered = els.filter(function(){
       return (this.value.length >= 4); 
    });
    $('#some-button').prop('disabled', (els.length != filtered.length));
});

http://jsfiddle.net/9se4x2an/ http://jsfiddle.net/9se4x2an/

The fact is, I've been staring at this for 3 hours and it was just a typo (misspelled length as lengh on one of the variables). 事实是,我一直在盯着它看了3个小时,这只是一个拼写错误( 长度拼写错误,其中一个变量是lengh )。 Needed more coffee to see it apparently. 显然需要更多的咖啡才能看到它。 Thanks to everyone for helping. 感谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM