在必填字段有效之前禁用按钮

Disable button until required fields are valid

我已经搜索了这个问题的答案,但仍然无法解决这个问题:

<script type="text/javascript"> 
$(document).ready(function (){
    validate();
    $('#contact_subject').change(validate);
});

function validate(){
    if ($('#contact_subject').val().length   >   0  {
        $('button').prop('disabled', false);
    }
    else {
       $('button').prop('disabled', true);
    }
}

</script>

我想确保 #contact_subject 有数据,否则禁用按钮。

我认为您在 if 条件

中缺少括号 )
function validate(){
    if ($('#contact_subject').val().length   >   0)  {
        $('button').prop('disabled', false);
    }
    else {
       $('button').prop('disabled', true);
    }
}

将 event.Try 更改为:http://jsfiddle.net/e0t7qv56/1/

$("#field").keyup(function(){
    console.log($(this).val().length);
    if($(this).val().length)
        $("#btn").prop('disabled', false);
    else
        $("#btn").prop('disabled', true);
});