我试图保持输入禁用,直到2个输入至少有4个字符.我能在SO上找到的最接近的是一个用户总共8个字符,但是这将允许其中一个输入为空白而另一个超过8.这是我尝试过的:
没有 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);
}
});
Run Code Online (Sandbox Code Playgroud)
同 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);
}
});
Run Code Online (Sandbox Code Playgroud)
有趣的是,我可以console.log(foo.length)并且console.log(bar.length)看到每个长度都很好keyup.
正如A. Wolff已经提到的,您的情况是使用>代替>=.
if (foo.length >= 4 && bar.length >= 4) {
Run Code Online (Sandbox Code Playgroud)
如果您有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);
});
Run Code Online (Sandbox Code Playgroud)
从概念上讲,这意味着"如果任何输入不是4个字符,则禁用",而不是"如果两个输入都超过4个字符,则禁用".
| 归档时间: |
|
| 查看次数: |
1187 次 |
| 最近记录: |