0

javascript:

$("#id_report").on('change', function() {
   var reportform = document.getElementById("status-form");
   var live = document.getElementById("id_report");
   if (live.checked == true){
    reportform.submit();
   }
  });

forms.py

class Reportform(Form):
    report = forms.BooleanField(widget=forms.CheckboxInput(),required=False, label="Report",initial=True)

template.html

<form action="." id="status-form" method="POST">{% csrf_token %}
  <p>{{closedreport.report}}Show Reports</p>
  </form>

I want to submit the form if check box is checked.By default the check box is checked,but form is not getting submitted.

2
  • 'By default the check box is checked', so you want to submit the form instantly? Commented Aug 2, 2013 at 12:08
  • @user2086641 and why on earth would you like to do that? :D Maybe you are trying to achieve something that can be done in a much simpler and more logiacl. Commented Aug 2, 2013 at 12:32

2 Answers 2

1

Try this:

$("#id_report").click(function(){
    if($(this).is(':checked'){
        $("#status-form").submit();
    }
});

When the button is clicked, the script checks if checkbox is checked. If it is, submit the form.

Sign up to request clarification or add additional context in comments.

Comments

1

To submit your form instantly (and i'm guessing you want to have that happen when the page where your form is on loads) you could do :

// Wait for document to load
$(function(){
    // Check if your checkbox is indeed checked
    if($('#id_report').is(':checked'){
        // Submit the form
        $("#status-form").submit();
    }
});

Or you could do it like this:

$(function(){
    $("#status-form").on('change', '#id_report', function() {
        var reportform = document.getElementById("status-form");
        var live = document.getElementById("id_report");
        if (live.checked == true){
            reportform.submit();
        }
    }).trigger('change');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.