javascript - How to submit checkbox through jquery ajax? -
i have difficulty submitting 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 jquery bit:
$('.mychoice').click( function() { $.ajax({ url: '/someurl', type: 'post', datatype: 'json', success: function(data) { // ... data... } }); });
but nothing happens when click checkbox. how can fix this?
update: may worth mentioning form located @ bootstrap modal.
you're missing data
property.
see: jquery $.ajax() post - data in java servlet example.
if want send contents of form, use form.serialize()
, put whatever data want property.
$(document).ready(function() { $('.mychoice').click(function() { var formdata = $('#myform').serialize(); console.log('posting following: ', formdata); $.ajax({ url: '/someurl', data: formdata, type: 'post', datatype: 'json', success: function(data) { // ... 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>
Comments
Post a Comment