Advertisement
Guest User

Untitled

a guest
Nov 18th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery(document).ready(function($) {
  2.  
  3.     // Vars
  4.     var post_type = pa_vars.pa_type;
  5.     var selector_country = '';
  6.     var selector_area = '';
  7.  
  8.     /**
  9.      * Define selectors for all 3 post types, ID's of Select Country and Select Area select menu
  10.      *
  11.      * I have 3 different post types, with different ID's of select tag so I need to do this to append options to
  12.      * corrett one
  13.      */
  14.     if( post_type == 'reserves' ) {
  15.         selector_area = 'acf-field-reserve_area';
  16.         selector_country = 'acf-field-reserve_country';
  17.     }
  18.     else if( post_type == 'hotspots' ) {
  19.         selector_area = 'acf-field-hotspot_area';
  20.         selector_country = 'acf-field-hotspot_country';
  21.     }
  22.     else if( post_type == 'ait-dir-item' ) {
  23.         selector_area = 'acf-field-lodge_area';
  24.         selector_city = 'acf-field-lodge_nearest_major_city';
  25.         selector_country = 'acf-field-lodge_country';
  26.     }
  27.  
  28.     else if( post_type == 'drongo' ) {
  29.         selector_area = 'safari_area';
  30.         selector_city = 'nearest_major_city';
  31.         selector_country = 'select_country';
  32.     }
  33.  
  34.     $( '#' + selector_area ).attr( 'disabled', 'disabled' );
  35.    
  36.  
  37.     /**
  38.      * Get country options
  39.      * On change of 'select country' get country selected
  40.      */
  41.     $( '#' + selector_country ).change(function () {
  42.  
  43.         var selected_country = '';
  44.  
  45.         if( post_type == 'drongo' ) {
  46.             ajaxurl = pa_vars.ajaxurl;
  47.             $( '#' + selector_city ).attr( 'disabled', 'disabled' );
  48.             $( '#' + selector_city ).html( $('<option></option>').val('0').html('Please select one of the following') );
  49.         }
  50.  
  51.         $( '#' + selector_country + ' option:selected' ).each(function() {
  52.             selected_country += $( this ).text() + '';
  53.         });
  54.  
  55.        
  56.         // Get data based on selected country
  57.         data = {
  58.             action: 'pa_add_areas',
  59.             pa_nonce: pa_vars.pa_nonce,
  60.             country: selected_country,
  61.         };
  62.  
  63.         var selected_area = $( '#' + selector_area + ' option:selected').val();
  64.         var selected_city = $( '#' + selector_city + ' option:selected').val();
  65.  
  66.         alert( selected_area );
  67.  
  68.         $.post( ajaxurl, data, function(response) {
  69.            
  70.             if(response){
  71.                 var areas = response['area'];
  72.                 var cities = response['city'];
  73.  
  74.                 if( post_type == 'drongo' || post_type == 'ait-dir-item') {
  75.  
  76.                     $( '#' + selector_city ).html( $('<option></option>').val('0').html('Please select one of the following').attr('disabled','disabled') );
  77.  
  78.                     $.each(cities, function(val, text) {
  79.                         $( '#' + selector_city ).append( $('<option></option>').val(text).html(text) )
  80.                     });
  81.  
  82.                     $( '#' + selector_city + ' option[value="' + selected_city +'"]').attr('selected','selected');
  83.                 };
  84.  
  85.                 //$( '#' + selector_area ).html( $('<option></option>').val('0').html('Please select one of the following').attr('disabled','disabled') );
  86.                 $.each(areas, function(val, text) {
  87.                     $( '#' + selector_area ).append( $('<option></option>').val(text).html(text) )
  88.                 });
  89.  
  90.  
  91.                 $( '#' + selector_area ).removeAttr( 'disabled' );
  92.                 //$( '#' + selector_area + ' option[value="' + selected_area +'"]').attr('selected','selected');
  93.  
  94.                 if ( post_type == 'drongo' ) {
  95.                     $( '#' + selector_city ).removeAttr( 'disabled' );
  96.                 };
  97.             };
  98.         });
  99.  
  100.         return false;
  101.     }).change();
  102. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement