2013-01-10 232 views
0

我有这个“显示密码”复选框,切换密码的可见性,它工作正常,但我只是想禁用密码字段为空时的复选框,只有在输入某个时间时启用。检查文本框是否为空

http://jsfiddle.net/aaK9E/2/

var showPass=false; 
$('#c').change(function(){ 
    showPass = ($('#c:checked').length>0); 
    if (showPass){ 
     $('#p').hide(); 
     $('#t').show(); 
    }else{ 
     $('#t').hide(); 
     $('#p').show(); 
    } 
}); 

$('#p').change(function(){ 
    if (!showPass) $('#t').val($('#p').val()); 
}); 
$('#t').change(function(){ 
    if (showPass) $('#p').val($('#t').val()); 
}); 

我试图KEYUP和变化,但如果用户写道有时它不会帮助,然后删除了它,该复选框将保持启用状态。

回答

2

试试这个:http://jsfiddle.net/aaK9E/51/

var showPass = false; 
$('#c').attr('disabled', 'disabled'); // disabled on doc ready 
$('#c').change(function() { 
    showPass = ($('#c:checked').length > 0); 
    if (showPass) { 
     $('#p').hide(); 
     $('#t').show(); 
    } else { 
     $('#t').hide(); 
     $('#p').show(); 
    } 
}); 

$('#p').keypress(function() { // use keypress instead of change 
    $('#c').removeAttr('disabled'); // remove the attr 'disabled' 
    if (!showPass) $('#t').val($('#p').val()); 
}); 
$('#t').change(function() { 
    if (showPass) $('#p').val($('#t').val()); 
}); 

你可以改变这个如果backspace is done和密码字段不具有任何价值:

$('#p').keyup(function() { 
    $('#c').removeAttr('disabled'); 
    if (!showPass) { 
    $('#t').val($('#p').val()); 
    } 
    if ($(this).val() == '') { // this will check the val if there is not 
    $('#c').attr('disabled', 'disabled'); it will disable it again. 
    } 
}); 

如果鲈jQuery是使用:

$('#p').on('keyup', function() { // this '.on()' requires latest jquery 
    $('#c').removeAttr('disabled'); 
    if (!showPass) { 
    $('#t').val($('#p').val()); 
    } 
    if ($(this).val() == '') { // this will check the val if there is not 
    $('#c').attr('disabled', 'disabled'); it will disable it again. 
    } 
}); 

如果需要try to use latest jquery

+0

之后我删除所有字符,并集中了,它使复选框启用 – 3zzy

+0

PLZ看到更新后的小提琴http://jsfiddle.net/aaK9E/51/。并回答更新。 – Jai

0

可以使用的keydown以检查框为空或不:

$('#p').keydown(function(){ 
    if($(this).val() == ""){ 
     $('#c').attr("disabled", true); 
    }else{ 
     $('#c').removeAttr("disabled"); 
    } 
}); 

上的一个键了,它会检查文本框为空,如果是,它会禁用复选框。 然后你只需要做if(){检查页面加载的初始状态。

1

使用下面的代码。 JSFiddle demo

将禁用不论状态(即在文本框或密码框中输入)

$('#p, #t').keyup(function(){ 
    if ($(this).val() != "") { 
    Enable(); 
    } 
    else 
    { 
    Disable(); 
    } 
}); 

function Enable() { 
$("#c").removeAttr("disabled"); 
} 

function Disable() { 
    $("#c").attr("disabled", true); 
} 
0

试试这个: - http://jsfiddle.net/aaK9E/52/ 这也将是第一次合作时的密码字段为空。

<input type="text" size="50" id="t" /> 
<input type="password" size="50" id="p" /><br /> 
<input type="checkbox" id="c" disabled="true">Show Password 



var showPass=false; 
$('#c').change(function(){ 
    showPass = ($('#c:checked').length>=0); 
    if (showPass){ 
     $('#p').hide(); 
     $('#t').show(); 
    }else{ 
     $('#t').hide(); 
     $('#p').show(); 
    } 
}); 

$('#p').bind("change keyup keypress keydown",function(){ 
    if (!showPass) $('#t').val($('#p').val()); 
    if($(this).val()=='') 
    $('#c').attr("disabled","true"); 
    else 
    $('#c').removeAttr("disabled"); 
}); 
$('#t').bind("change keyup keypress keydown",function(){ 
    if (showPass) $('#p').val($('#t').val()); 
    if($(this).val()=='') 
    $('#c').attr("disabled","true"); 
    else 
    $('#c').removeAttr("disabled"); 
});