【问题标题】:jQuery Validation Field AutoFills Another, Removing Error without clicking fieldjQuery 验证字段自动填充另一个,无需单击字段即可删除错误
【发布时间】:2015-02-24 19:08:15
【问题描述】:

我有自动填充其他文本框的字段,例如,如果选择了显示“礼物”的下拉菜单,它会填充具有相同值的文本框,即“礼物”。

我无法理解的是 jQuery 的验证。如果文本框为空并且您单击提交,它将告诉您此字段是必需的并且 css 为红色(我为错误设置)但是当您然后选择下拉值礼物并且它预填充文本框时它不会删除错误直到您单击文本框或通过它标签。

有没有人遇到过这个问题或发现了自动填充其他字段的字段的解决方案?

编辑

我也有这个地理定位功能,它会在输入时自动填写地址,做同样的事情。我试图弄清楚我可以在哪里添加 .valid() 以便一旦选择了地址并预先填写了城市、州和 zip,它也会删除错误 css。

var placeSearch, autocomplete;
var componentForm = {
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  postal_code: 'short_name'
};

function initialize() {
  // Create the autocomplete object, restricting the search
  // to geographical location types.
  autocomplete = new google.maps.places.Autocomplete(
    /** @type {HTMLInputElement} */
    (document.getElementById('autocomplete')), {
      types: ['geocode']
    });
  // When the user selects an address from the dropdown,
  // populate the address fields in the form.
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    fillInAddress();
  });
}

// [START region_fillform]
function fillInAddress() {
  // Get the place details from the autocomplete object.
  var place = autocomplete.getPlace();

  for (var component in componentForm) {
    document.getElementById(component).value = '';
    document.getElementById(component).disabled = false;
  }

  // Get each component of the address from the place details
  // and fill the corresponding field on the form.
  for (var i = 0; i < place.address_components.length; i++) {
    var addressType = place.address_components[i].types[0];
    if (componentForm[addressType]) {
      var val = place.address_components[i][componentForm[addressType]];
      document.getElementById(addressType).value = val;
    }
  }
  //var keys=[];for (var key in place.address_components[0]) keys.push(key);
  //alert(keys):
  document.getElementById('autocomplete').value = 
    place.address_components[0]['long_name'] + ' ' +
    place.address_components[1]['long_name'];

  /*document.getElementById('route').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete').value : '');*/
  document.getElementById('route').value = '';
}
  // [END region_fillform]

// [START region_geolocation]
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = new google.maps.LatLng(
        position.coords.latitude, position.coords.longitude);
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}

  // [END region_geolocation]
initialize();

http://jsfiddle.net/bobrierton/89nqv5v5/

【问题讨论】:

  • "...我无法理解是 jQuery 的验证" ~ jQuery 没有任何验证。你在说the jQuery Validate plugin吗?
  • 向我们展示您的代码。 我不明白您为什么需要验证用户无法直接填写的字段。由于您以编程方式控制数据,因此它应该始终有效。

标签: jquery jquery-validate


【解决方案1】:

jQuery Validate 插件的数据验证测试通常仅由正常的用户事件触发,如跳格/单击 (onfocusout)、键入 (onkeyup) 等。

如果您以编程方式填写字段,则必须以编程方式触发验证测试。

在您用于填写该字段的任何代码中,将the .valid() method 附加到您要验证的字段。

否则,您可以在字段本身的值发生变化时触发它...

$('#myField').on('change', function() {
    $(this).valid();
});

或选择更改的值...

$('#mySelect').on('change', function() {
    $('#myField').valid();
});

但是,我不明白您为什么需要验证用户无法直接填写的字段。由于您以编程方式控制数据,因此它应该始终有效。

【讨论】:

  • @DavidBrierton,是的,但无论如何,你应该在你的 OP 中显示你的代码。
  • @DavidBrierton,我只从经验中知道答案。但是 SO 被维护为每个人的存储库。因此,阅读您的问题的其他人可能也无法理解。顺便说一句:SO 指南规定您应该包含足够的相关代码来演示该问题,并且它也被列为结束问题的官方原因。
  • @DavidBrierton,我不确定您如何将其与 Google 地图集成。但是,原理是完全一样的。找到发生变化的字段并在 change 事件上触发 .valid()
  • @DavidBrierton,没有这种方法,也不需要。 .valid() 字段无效时为false,字段有效时为true
猜你喜欢
  • 2017-08-14
  • 2011-02-25
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 1970-01-01
  • 2020-04-24
  • 2018-01-31
  • 2011-03-18
相关资源
最近更新 更多