检测由邮政编码查找自动填充的输入字段中的值更改

时间:2013-04-02 17:03:46

标签: jquery validation

我在我的网站上使用邮政编码查询服务(https://www.postcodeanywhere.co.uk)。该脚本为我提供了一个输入字段,接受邮政编码,地址等,然后显示适当的结果。一旦我选择了正确的地址,它就会自动填充我确定的剩余输入字段(门牌号,街道等)。

如果填写正确,我想验证这些字段(在右边显示一个图标),但问题是它没有检测到值的变化。如果我再次单击填充的输入字段内部,它会启动验证。

我尝试过.change(),. bind(),keyup()和其他事件,但无法让它工作。还有其他想法吗?

更新:这是代码:

$('#postcode').bind('blur', function(){
   validatePostCode();
});

function validatePostCode() {
  var postCode = $('#postcode').val();

  if( !alphaNumericRegex.test(postCode) ) {
    $('#postCodeWrap').find('.errorMessage').html('Must contain six characters.);
    $('#postCodeWrap').find('.validationSuccess').hide();
    $('#postcode').css('background', 'rgb(242, 222, 222)');
    $('#postcode').addClass('errorVisible');
    return false;
  } else {
    $('#postCodeWrap').find('.validationSuccess').show();
    $('#postCodeWrap').find('.errorMessage').html('');
    $('#postcode').css('background', '#FFFFFF');
    $('#postcode').removeClass('errorVisible');
    return true;
  }
}

1 个答案:

答案 0 :(得分:4)

当使用JS以编程方式更改值时,不会触发任何事件。您可以在第一个字段更改时自行触发事件:

$('#first-field').on('change', function() {
    $('.the_rest_of_my_fields').trigger('change');
});