使用jQuery启用 /禁用输入



我有这样的代码,当我填写#number时,启用#texth(默认是禁用(以供我填写。但是,当我在#number中删除内容时。它不会禁用#texth!

 function soluong_nhap()
    {
        $('#customers2').find('tr').click( function(){
            var row = $(this).find('#number').val();
            if(row != null)
            {
                $('#texth').prop("disabled", false);
            }
            if(row == null)
            {
                $('#texth').prop("disabled", true);
            }
        });
    }

删除内容时,它不会变成null,它是 ""。您应该将其添加到您的病情中,还应使用else

function soluong_nhap()
{
    $('#customers2').find('tr').click( function(){
        var row = $(this).find('#number').val();
        if (row === null || row === "")
        {
            $('#texth').prop("disabled", true);
        }
        else
        {
            $('#texth').prop("disabled", false);
        }
    });
}

,因为当您删除所有内容时,#texth的值将变为"(空字符串(

尝试这个

function soluong_nhap()
{
    $('#customers2').find('tr').click( function(){
        var row = $(this).find('#number').val();
        $('#texth').prop("disabled", !row);
    });
}

尝试使用keyup((函数。

$(row).keyup(function() {
 if(row != null)
            {
                $('#texth').prop("disabled", false);
            }
            if(row == null)
            {
                $('#texth').prop("disabled", true);
            }
}
if (row === null || row === "")
    {
        $('#texth').prop("disabled", true);
    }
    else
    {
        $('#texth').prop("disabled", false);
    }

您可以简化为:

$('#texth').prop("disabled", (row === null || row === ""));

最新更新