Advertisement
kellykamay

request post

Jun 29th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $('form').submit((e) => {
  2.     let jsonObj = [];
  3.     let jsonChap = {};
  4.     let iddates = [];
  5.  
  6.     e.preventDefault();
  7.  
  8.     //get howmany .chapters
  9.     let howmanyDays = $('.chapters').length;
  10.     //get id (which is a date) of each chapter then pass it to our array
  11.     for (i=0; i<howmanyDays; i++){
  12.         iddates.push($('.chapters').eq(i).attr('id'));
  13.     }
  14.     iddates.sort();
  15.     $('#story_date').val(iddates[0]);
  16.      console.log(iddates.length);
  17.  
  18.     for (i=0; i<iddates.length; i++){
  19.                
  20.             // It is also possible to iterate through all elements within a specific context, no mattter how deeply nested they are:
  21.             // The second parameter $('#mydiv') which is passed to the jQuery '.subevent' Selector is the context. In this case the each() clause will iterate through all input elements within the '#'+iddates[i] container, even if they are not direct children of #mydiv.
  22.             $('.subevent', $('#'+iddates[i])).each(function(){
  23.                 jsonChap = {};
  24.                 jsonChap.date = iddates[i];
  25.                 $('input.subtitle', $(this)).each(function(){
  26.                     // console.log($(this).val());
  27.                     jsonChap.title = $(this).val();
  28.                 });
  29.                 $('.subdescription', $(this)).each(function(){
  30.                     // console.log($(this).val());
  31.                     jsonChap.description = $(this).val();
  32.                 })
  33.                 jsonObj.push(jsonChap);
  34.             });
  35.            
  36.     }
  37.     console.log(JSON.stringify(jsonObj));
  38.     $('#chapters_jsonfield').val(JSON.stringify(jsonObj));
  39.  
  40.     let data = $('form').serialize();
  41.     $.post('/api/story/', data).success((response) => {
  42.         // Andito yun response ng DRF
  43.         console.log(response.content);
  44.         // Baguhin mo na lang kung ano kailangang palitan sa page
  45.         $('form').html('<p>Success</p>');
  46.   });
  47.  
  48. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement