简体   繁体   中英

How to enable/disable button with checkbox using JS inside Django crispy form?

I am trying to implement this: http://jsfiddle.net/8YBu5/7/

I would like to enable / disable a django crispy forms submit button based on a checkbox in the same crispy form.

Is it possible? how do I get this JS to work?

The JS doesn't seem to do anything. I feel like I am missing something basic here...

here is my crispy form:

email = forms.EmailField(label=_("Email"))
password1 = forms.CharField(widget=forms.PasswordInput,label=_("Password"))
billing_secret = forms.CharField()
termsandcond = forms.TypedChoiceField(
        label = False,
        choices = ((1, "Yeah sure"),),
        initial = '0',
    )

def __init__(self, *args, **kwargs):
    billing_secret = kwargs.pop('billing_secret', None)
    super(RegistrationForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_method = 'post'
    self.helper.form_action = '.'

    self.helper.layout = Layout(
        Field('email', placeholder=_("Email")),
        Field('password1', placeholder=_("Password")),
        InlineCheckboxes('termsandcond'),
        Submit("save", _("Get Started"),css_class="pull-right", css_id="postme"),
    )

here is my JS:

$('#id_termsandcond_1').click(function(){

    if($(this).attr('checked') == false){
         $('#postme').attr("disabled","disabled");   
    }
    else {
        $('#postme').removeAttr('disabled');
    }
});

here are the relevant rendered django crispy form elements:

<div id="div_id_termsandcond" class="form-group">
    <div class="controls ">
        <label class="checkbox-inline">
            <input type="checkbox" name="termsandcond" id="id_termsandcond_1" value="1">Yeah sure
        </label>
    </div>
</div>

<input type="submit" name="save" value="Get Started" class="btn btn-primary pull-right" id="postme">

This works. You need to use .prop('checked').

<html>
<body>
    <form action="/somehandler.html" method="get">
        <div id="div_id_termsandcond" class="form-group" >
            <div class="controls ">
                <label class="checkbox-inline">
                    <input type="checkbox" name="termsandcond" id="id_termsandcond_1" value="1">Yeah sure
                </label>
            </div>
        </div>
        <input type="submit" name="save" value="Get Started" class="btn btn-primary pull-right" id="postme">
    </form>

    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script type="text/javascript">

        $('#postme').attr('disabled', 'disabled');
        $('#id_termsandcond_1').click(function(){
            if($(this).prop('checked')  === false){
                $('#postme').attr('disabled', 'disabled');
            }else {
                $('#postme').removeAttr('disabled');
            }
        });
    </script>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM