Вы можете отслеживать один вход для изменения, а затем в зависимости от его содержимого, соответствующего заданному значению, обновлять второй.
Тривиальный пример:
<label for="code">Code:</label> <input type="text" id="code" />
<label for="area">Area:</label> <input type="text" id="area" />
<script type='text/javascript'>
$(document).ready(function(){
// Listen for change on our code input
$('#code').keyup(function(){
var entered = $(this).val(); // text entered in 'code'
// Look up the start of the code (first 6 digits?)
if(entered.substring(0,6) == '1Z8473')
{
// Set the value of our "area" input
$('#area').val('Houston, TX');
}
});
});
</script>
В приведенном выше примере для сопоставления используется простой блок if
. Как и другие, размещенные здесь, вы можете отправить это значение в сценарий на стороне сервера, который выполняет геолокацию для необходимых областей. Таким образом, заменив if
выше на что-то вроде:
// Look up the area depending on the code entered
$.getJSON(
'/geocoder_on_your_site.php?area='+entered,
function(data) {
$('#area').val(data.location);
});
и на вашем сервере, в geocoder_on_your_site.php
:
$input = $_REQUEST['entered'];
// Assume input is sanitised & checked first..
...
// Look up the area base on location
$data['location'] = look_up_location($input);
echo json_encode($data);