Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Полный адрес в одном поле. Результат - почтовый индекс, регион, город, улица и дом автоматически разложить по скрытым полям.


// Ваш api ключ из лк дадаты https://dadata.ru/#registration_popup
var token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

function join(arr /*, separator */) {
  var separator = arguments.length > 1 ? arguments[1] : ", ";
  return arr.filter(function(n){return n}).join(separator);
}

function showPostalCode(address) {
  $("#shipping_address_postcode").val(address.postal_code);
}

function showRegion(address) {
  $region = $("#shipping_address_zone_id");
  
  if ($region.length 
      && typeof(address.region) !== 'undefined' 
     	&& address.region) {
    		var result = address.region.replace(/\s{0,1}[-\/].+/gi, '').toLowerCase();
    		$region.find('option').each(function (i, o) {
          if ($(o)
              .text()
              .toLowerCase()
              .search(result) >= 0
             )
            $(this).attr('selected', true);
        	else
            $(this).attr('selected', false);
        });
  };
}

function showCity(address) {
  $("#shipping_address_city").val(join([
    join([address.city_type, address.city], " "),
    join([address.settlement_type, address.settlement], " ")
  ]));
}

function showAddress(address) {
  insert = '';

  if (typeof(address.settlement) !== 'undefined'
     	&& address.settlement)
    insert += join([address.settlement_type, address.settlement], " ")
  
  insert += join([
    join([address.street_type, address.street], " "),
    join([address.house_type, address.house], " ")
  ])
  
  if (parseInt(address.block) > 0)
    insert += join([address.block_type, address.block], " ");
  
  if (parseInt(address.flat) > 0)
    insert += join([address.flat_type, address.flat], " ");
  
  $("#shipping_address_address_1").val(insert);
}

// function showStreet(address) {
//   $("#shipping_address_street").val(
//     join([address.street_type, address.street], " ")
//   );
// }

// function showHouse(address) {
//   $("#house").val(join([
//     join([address.house_type, address.house], " "),
//     join([address.block_type, address.block], " ")
//   ]));
// }

// function showFlat(address) {
//   $("#flat").val(
//     join([address.flat_type, address.flat], " ")
//   );
// }
  
var cityChanged = 0;
function inputChanged(a,b,c) {
  cityChanged = 1
}

function showSelected(suggestion) {
  var address = suggestion.data;
  showPostalCode(address);
  showRegion(address);
  showCity(address);
  showAddress(address);
  // showStreet(address);
  // showHouse(address);
  // showFlat(address);
  
  if (cityChanged) 
    reloadAll()
}

$("#shipping_address_address_2").suggestions({
  token: token,
  type: "ADDRESS",
  onSelect: showSelected,
  beforeRender: inputChanged
});

 


Comments

There are no comments to display.

×
×
  • Create New...

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.