根据输入的邮政编码填充州和城市?

时间:2018-07-18 14:01:47

标签: javascript jquery html5 twitter-bootstrap closest

我有一个可以在其中输入邮政编码的表格,根据该表格,城市和州将自动填充。因此,这三个字段彼此之间是相同的形式。这是我的表格示例:

$('.get-zip').keyup(getZipCode);
function getZipCode(){
    var fldZip = $(this);
    
    console.log($(this).closest('.form-group'));
   
    if(fldZip.val().length === 5 && fldZip.val().match(/^[0-9]+$/)) {
        //Send Ajax request and populate closest State and City fields.
    }else{
        //$(cityID).val('');
        //$(stateID).val('');
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<form name="frmSave" id="frmSave" autocomplete="off">
  <div class="form-group">
    <label class="control-label" for="city"><span class="label label-primary">City:</span></label>
    <input type="text" class="form-control city" name="frm_city" id="frm_city" maxlength="50" placeholder="City auto populate based on the zip code" readonly>
  </div>
  <div class="form-group">
    <label class="control-label" for="state"><span class="label label-primary">State:</span></label>
    <input type="text" class="form-control state" name="frm_state" id="frm_state" maxlength="2" placeholder="State auto populate based on the zip code" readonly>
  </div>
  <div class="form-group">
    <label class="control-label" for="zip"><span class="label label-primary">Zip:</span></label>
    <input type="text" class="form-control get-zip" name="frm_zip" id="frm_zip" maxlength="5" placeholder="Enter 5 digits ZIP code" pattern="[0-9]{5}$" required>
  </div>
  <div class="row">
    <div class="form-group col-xs-12 col-sm-12 col-md-12 col-lg-12">
      <button type="submit" name="frm_submit" id="frm_submit" class="btn btn-primary">Submit</button>
    </div>
    <div class="form-group col-xs-12 col-sm-12 col-md-12 col-lg-12">
      <div id="frmSave_message"></div>
    </div>
  </div>
</form>

此功能在我的应用程序中以其他几种形式使用,因此我需要以任何形式使用它。我想知道在Zip输入字段上方填充州和市的最简单方法是什么?我尝试使用closest(),但由于我需要指定需要填充的输入,因此无法使用。如果有人对如何实现这一目标有更好的想法,请告诉我。谢谢。

3 个答案:

答案 0 :(得分:3)

这将始终发布到最接近的州和城市输入形式! 希望我能帮上忙!

$('.get-zip').keyup(getZipCode);
function getZipCode(){
    var fldZip = $(this);
 
    // Testing Purpose
    $(this).closest("form").find(".state").val($(this).val());
    $(this).closest("form").find(".city").val($(this).val());
   
    if(fldZip.val().length === 5 && fldZip.val().match(/^[0-9]+$/)) {
        
       $(this).closest("form").find(".state").val("YOUR VALUE");
       $(this).closest("form").find(".city").val("YOUR VALUE");

    }else{
        //$(cityID).val('');
        //$(stateID).val('');
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<form name="frmSave" id="frmSave" autocomplete="off">
  <div class="form-group">
    <label class="control-label" for="city"><span class="label label-primary">City:</span></label>
    <input type="text" class="form-control city" name="frm_city" id="frm_city" maxlength="50" placeholder="City auto populate based on the zip code" readonly>
  </div>
  <div class="form-group">
    <label class="control-label" for="state"><span class="label label-primary">State:</span></label>
    <input type="text" class="form-control state" name="frm_state" id="frm_state" maxlength="2" placeholder="State auto populate based on the zip code" readonly>
  </div>
  <div class="form-group">
    <label class="control-label" for="zip"><span class="label label-primary">Zip:</span></label>
    <input type="text" class="form-control get-zip" name="frm_zip" id="frm_zip" maxlength="5" placeholder="Enter 5 digits ZIP code" pattern="[0-9]{5}$" required>
  </div>
  <div class="row">
    <div class="form-group col-xs-12 col-sm-12 col-md-12 col-lg-12">
      <button type="submit" name="frm_submit" id="frm_submit" class="btn btn-primary">Submit</button>
    </div>
    <div class="form-group col-xs-12 col-sm-12 col-md-12 col-lg-12">
      <div id="frmSave_message"></div>
    </div>
  </div>
</form>

答案 1 :(得分:1)

只要所有不同形式的元素以相同的方式排列,以下内容可能会起作用。

jQuery .prev()

jQuery .find()

var stateFormGroup = $(this).closest('.form-group').prev();
var stateElement = stateFormGroup.find('input');
var cityElement = stateFormGroup.prev().find('input');

答案 2 :(得分:1)

在此处发表评论作为答案。

$(this).closest('.form-group')将选择父表单组。尝试选择最接近的表单,然后遍历到表单组。

$form_group = $(this).closest('form').find('.form-group'); 
$form_group.find('input.city').val(city); 
$form_group.find('input.state').val(state);