取消选中复选框后返回"on"值

use*_*510 2 javascript checkbox jquery attributes

我正在按照一个教程,您可以在其中创建一个启用禁用按钮的复选框,当您取消选中该框时,该按钮将再次被禁用.问题是,当我取消选中该框时,似乎复选框仍处于"开启"状态,因为该按钮未被禁用.该代码适用于Chrome,但不适用于Firefox 3.6.

这是我的代码:

<p><input type="checkbox" id="agree" >I agree</p>
<input id="continue" type="button" value="continue" disabled='true'>


<script>
        $('#agree').change(function(){
       var state = $(this).attr('value');
        if(state == 'on'){
        $('#continue').removeAttr('disabled');
        }else if(state == ''){
            $('#continue').attr('disabled', 'false');
        }
        });
    </script>
Run Code Online (Sandbox Code Playgroud)

Mad*_*iha 7

复选框的检查值不会改变,.val()你应该.is('checked')像这样使用:

$('#agree').change(function() {
    var state = $(this).is(':checked'); //state = true/false depending on state
    if (state) { //if true
        $('#continue').removeAttr('disabled'); //remove disabled
    } else { //else (false)
        $('#continue').attr('disabled', 'disabled'); //add disabled (you shouldn't set it to 'false' as it WILL be disabled and it'll confuse you.
    }
});
Run Code Online (Sandbox Code Playgroud)

这是一个看我的观点的例子.