Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. $(document).ready(function () {
  2.  
  3. let typeChanged = false;
  4.  
  5. $('.select2-class').select2({
  6. placeholder: 'Select option'
  7. });
  8.  
  9. $('#category_select').on("change", function () {
  10. //getting the category id
  11. if (!typeChanged) {
  12. let selected_category_id = $("#category_select option:selected").val();
  13.  
  14. $.ajaxSetup({
  15. headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
  16. });
  17.  
  18. $.ajax({
  19. url: "{{ url('/category/change') }}",
  20. method: 'post',
  21. data: {
  22. id: selected_category_id,
  23. },
  24. success: function (result) {
  25.  
  26. $("#type_select").html('').select2({
  27. data: result.success
  28. });
  29. // console.log(result.success);
  30. }
  31. });
  32. }
  33.  
  34. }); // end of on change category select
  35.  
  36.  
  37. $('#type_select').on("change", function () {
  38. typeChanged = true;
  39. let selected_type_id = $("#type_select option:selected").val();
  40.  
  41. $.ajaxSetup({
  42. headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
  43. });
  44.  
  45. $.ajax({
  46. url: "{{ url('/type/change') }}",
  47. method: 'post',
  48. data: {
  49. id: selected_type_id,
  50. },
  51. success: function (result) {
  52. //retrieving category id that belongs to that type
  53. //and setting the category to the option
  54. $('#category_select').val(result.success);
  55. $('#category_select').select2().trigger('change');
  56. typeChanged = false;
  57. }
  58. });
  59.  
  60. }); // end of on change category select
  61.  
  62.  
  63. }); //end of document ready function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement