如何在Django crispy表单中使用JS启用/禁用带复选框的按钮?

时间:2015-09-03 08:51:57

标签: javascript django forms twitter-bootstrap django-crispy-forms

我正在努力实现这个: http://jsfiddle.net/8YBu5/7/

我想基于同一个脆弱形式的复选框启用/禁用django crispy表单提交按钮。

有可能吗? 如何让这个JS工作?

JS似乎没有做任何事情。我觉得我错过了一些基本的东西......

这是我脆的形式:

EXECUTE('Call STP_CAF_PERSON(?,?,?)', @AVEMAIL OUTPUT, @ANCDPXID OUTPUT, @AVCDPURN OUTPUT) AT [CAFUAT]

这是我的JS:

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"),
    )

这里是相关的渲染django crispy表单元素:

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

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

1 个答案:

答案 0 :(得分:0)

这很有效。你需要使用.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>