2

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

1 回答 1

4

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

$('#first-field').on('change', function() {
    $('.the_rest_of_my_fields').trigger('change');
});
于 2013-04-02T17:06:49.713 回答