开发者

Disable or enable Submit button on Checkbox checked event

I want something like this but with a slight change. I want a Button to be enabled or disabled on Checkbox checked event, i.e. when checkbox is checked then and then only button should be enabled otherwise it is disabled. This should be done using jQuery Code not JavaScript.

As this 开发者_StackOverflow社区is MVC form so there is no Form ID.


$(function() {
    $('#id_of_your_checkbox').click(function() {
        if ($(this).is(':checked')) {
            $('#id_of_your_button').attr('disabled', 'disabled');
        } else {
            $('#id_of_your_button').removeAttr('disabled');
        }
    });
});

And here's a live demo.


This is old but worked for me with jquery 1.11

<script>
$(function() {
    $('#checkbox-id').click(function() {
        if ($(this).is(':checked')) {
            $('#button-id').removeAttr('disabled');
        } else {
            $('#button-id').attr('disabled', 'disabled');
        }
    });
});
</script>

the if instruction swapped with the else instruction and in the button html markup add disabled="disabled"

may help someone

Jquery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


$('#checkbox').click(function(){
    if($(this).is(':checked')){
        $('#submitButton').attr("disabled", "true");
    }else {
        $('#submitButton').removeAttr("disabled");
    }

});

... it works for me ...


Seriously? Why not just:

onclick="$('#submitButton').attr('disabled',$('#checkbox').is(':checked'));"


I know, I am a bit late to the party, but here's my working solution for my WP comment form:

// disable comment form until gdpr is checked
 if($('#id_of_gdpr_checkbox').length > 0 && $('#submit').length > 0){
     if(!$('#id_of_gdpr_checkbox').is(':checked')) {
        $('#submit').attr('disabled', 'disabled');
     }
     $('#id_of_gdpr_checkbox').click(function() {
        if ($(this).is(':checked')) {
            $('#submit').removeAttr('disabled');
        } else {
            $('#submit').attr('disabled', 'disabled');
        }
    });
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜