簡體   English   中英

如何在Django crispy表單中使用JS啟用/禁用帶有復選框的按鈕?

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

我正在嘗試實現這一點: http : //jsfiddle.net/8YBu5/7/

我想啟用/禁用基於同一脆脆窗體中的復選框的django脆脆窗體提交按鈕。

可能嗎? 我該如何使用此JS?

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

這是我的JS:

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

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

以下是相關的渲染django脆皮表單元素:

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

這可行。 您需要使用.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM