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



我在我的网站(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;
  }
}

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

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

最新更新