我正在尝试动态绑定ajax响应的验证以获取某些地址验证表单。我尝试了一些东西,但似乎无法让这个工作。这是我到目前为止得到的任何帮助,所有的console.log都在工作。
$('#profileAddresses form').each(function() {
console.log('each');
var _id = $(this).prop('id');
var _idNum = _id.split('-');
var _input = _idNum[1];
var line1 = 'profile' + _input + 'Line1';
var city = 'profile' + _input + 'City';
var state = 'profile' + _input + 'State';
var zip = 'profile' + _input + 'Zip';
var country = 'profile' + _input + 'Country';
$(this).validate({
onkeyup: false,
errorClass: 'error',
errorElement: 'div',
onfocusout: false,
errorPlacement: function(error, element) {
error.insertBefore(element);
},
rules: {
line1: {
required: true
},
city: {
required: true
},
state: {
required: true
},
zip: {
required: true,
minlength: 5
},
country: {
required: true
}
},
messages: {
line1: {
required: 'Please enter a street address.'
},
city: {
required: 'Please select a city.'
},
state: {
required: 'Please select a state.'
},
zip: {
required: 'Please enter a zip code.',
minlengh: 'Pleaes enter a valid zip code.'
},
country: {
required: 'Please select a country.'
}
}
});
});
答案 0 :(得分:0)
这似乎有效,因为其他人遇到了这个问题,只需在表单上设置验证,然后将规则添加到输入中。我为每个添加了一个类,因此我可以更改所需的消息或要求。
$('#profileAddresses form').each(function() {
$(this).validate({
onkeyup: false,
errorClass: 'error',
errorElement: 'div',
onfocusout: false,
errorPlacement: function(error, element) {
error.insertBefore(element);
}
});
var _id = $(this).prop('id');
$('#' + _id + ' input').each(function() {
if($(this).hasClass('profileAddressZip')) {
$(this).rules('add', {
required: true,
messages: {
required: 'Required msg.'
}
});
}
});
});