cirandas.net

ref: master

public/javascripts/city_state_validation.js


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(function($){
  autoCompleteStateCity($);
  $('[id$="_country"]').change(function(){
    autoCompleteStateCity($);
  })
})(jQuery);

function autoCompleteStateCity($) {
  var country_selected = $('[id$="_country"] option:selected').val()
  if(country_selected == "BR")
  {
    $('#state_field').autocomplete({
      source : function(request, response){
        $.ajax({
          type: "GET",
          url: '/account/search_state',
          data: {state_name: request.term},
          success: function(result){
            response(result);
          },
          error: function(ajax, stat, errorThrown) {
            console.log('Link not found : ' + errorThrown);
          }
       });
      },

      minLength: 3
    });

    $('#city_field').autocomplete({
      source : function(request, response){
        $.ajax({
          type: "GET",
          url: '/account/search_cities',
          data: {city_name: request.term, state_name: $("#state_field").val()},
          success: function(result){
            response(result);
          },
          error: function(ajax, stat, errorThrown) {
            console.log('Link not found : ' + errorThrown);
          }
        });
      },

      minLength: 3
    });
  }
  else
  {
    if ($('#state_field').data('autocomplete')) {
      $('#state_field').autocomplete("destroy");
      $('#state_field').removeData('autocomplete');
    }

    if ($('#city_field').data('autocomplete')) {
      $('#city_field').autocomplete("destroy");
      $('#city_field').removeData('autocomplete');
    }
  }
}