【发布时间】: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();
【问题讨论】:
-
"...我无法理解是 jQuery 的验证" ~ jQuery 没有任何验证。你在说the jQuery Validate plugin吗?
-
向我们展示您的代码。 我不明白您为什么需要验证用户无法直接填写的字段。由于您以编程方式控制数据,因此它应该始终有效。