1

I have difficulty submitting this form:

<form action="/someurl" method="post">
    <input type="hidden" name="token" value="7mLw36HxPTlt4gapxLUKWOpe1GsqA0I5"> 
    <input type="checkbox" class="mychoice" name="name" value="apple"> Apple
    <input type="checkbox" class="mychoice" name="name" value="orange"> Orange      
    <input type="checkbox" class="mychoice" name="name" value="pear"> Pear
  </form>

And the jquery bit:

$('.mychoice').click( function() {
    $.ajax({
        url: '/someurl',
        type: 'post',
        dataType: 'json',
        success: function(data) {
                 //  ... do something with the data...
                 }
    });
});

But nothing happens when I click a checkbox. How can I fix this?

UPDATE: It may worth mentioning that the form is located at a bootstrap modal.

Karlom
  • 13,323
  • 27
  • 72
  • 116
  • When are you running the jquery code? – Alejandro C. Nov 17 '16 at 19:46
  • It is supposed to run on a bootstrap modal. – Karlom Nov 17 '16 at 19:47
  • Are you sure that your elements are in the DOM when your code runs? Can you post a snippet example? – Alejandro C. Nov 17 '16 at 19:48
  • you need to include the data attribute in your ajax syntax. Also, I don't see where your attempting to get the actual value of the check box. I think this will help you. http://stackoverflow.com/questions/2834350/get-checkbox-value-in-jquery – Neo Nov 17 '16 at 19:50

5 Answers5

5

You're missing the data property.

See: JQuery $.ajax() post - data in a java servlet for an example.

If you want to send the contents of the form, then you would use Form.serialize(), but you could put whatever data you want into the property.

$(document).ready(function() {
  $('.mychoice').click(function() {
    var formData = $('#myForm').serialize();
    console.log('Posting the following: ', formData);
    
    $.ajax({
      url: '/someurl',
      data: formData,
      type: 'post',
      dataType: 'json',
      success: function(data) {
        //  ... do something with the data...
      }
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="/someurl" method="post" id="myForm">
  <input type="hidden" name="token" value="7mLw36HxPTlt4gapxLUKWOpe1GsqA0I5">
  <input type="checkbox" class="mychoice" name="name" value="apple">Apple
  <input type="checkbox" class="mychoice" name="name" value="orange">Orange
  <input type="checkbox" class="mychoice" name="name" value="pear">Pear
</form>
Community
  • 1
  • 1
Dan Wilson
  • 3,937
  • 2
  • 17
  • 27
0

Try this:

$(document).ready(function(){
    $('.mychoice').change( function() {
        $.ajax({
            url: '/someurl',
            type: 'post',
            dataType: 'json',
            success: function(data) {
                //  ... do something with the data...
            }
        });
    });
});
James McDowell
  • 2,668
  • 1
  • 14
  • 27
  • @Mister Positive In what way does it not answer the question. He asked what he can do; I said try this. That sounds like an answer to me, but then again, I've only spoken English my whole life. – James McDowell Nov 21 '16 at 12:25
0

supply missing data

$(document).ready(function() {
  $('.mychoice').click(function() {
    $.ajax({
      url: '/someurl',
      data: $(this).closest('form').serialize(),
      type: 'post',
      dataType: 'json',
      success: function(data) {
        //  ... do something with the data...
      }
    });
  });
});
Swatantra Kumar
  • 1,324
  • 5
  • 24
  • 32
0

if you are trying to receive the data with the csrf token do as below :

var fd = new FormData()
fd.append('csrfmiddlewaretoken', document.getElementsByName('csrfmiddlewaretoken')[0].value)
fd.append('field_name', $("input[name='field_name']").serialize())

will return

['field_name=9&field_name=15&field_name=10']

you will then have to parse the info in your view ( django )

-1

Try 'change' instead of 'click', like this:

$('.mychoice').change(function(){...})
evilReiko
  • 19,501
  • 24
  • 86
  • 102
  • I did, but has no effect. I even added a `alert("clicked")` inside the jquery function, but it is not triggered when I tick a checkbox. – Karlom Nov 17 '16 at 19:55
  • @Karlom in Chrome right-click then select "inspect element", you probably have javascript error which causes javascript to stop, OR, jquery is not included correctly – evilReiko Nov 17 '16 at 19:58