簡體   English   中英

自定義jQuery.validator規則出現問題

[英]Problem with custom jQuery.validator rule

我對此代碼有疑問,似乎無法解決:

$.validator.addMethod('street_address', function (value, element) {
  var address  = value + ', ' + $(element).parent().parent().find("#venue_city_id option:selected").text(),
      geocoder = new google.maps.Geocoder(),
      that     = this;
  geocoder.geocode({address: address}, function (results, status) {
    return that.optional(element) || status == google.maps.GeocoderStatus.OK;
  });
}, 'invalid address');

它可以使不正確的地址(Google無法對其進行地理編碼的地址)無效。 問題是即使返回true,我仍然會收到“無效地址”。 關於可能是什么問題的任何想法?

您從內部回調返回

geocoder.geocode({address: address}, function (results, status) {
    return that.optional(element) || status == google.maps.GeocoderStatus.OK;
});

不是來自驗證器功能。

此外, geocoder.geocode執行,我相信,AJAX的異步查詢,所以你不能就能夠告訴你離開時,驗證您的位置是否是“geocodeable”。 在jQuery驗證中,應該有一個針對此類情況的特定驗證器,稱為remote或諸如此類,該驗證器基於ajax查詢的結果進行驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM