我做了上述 答案中的工作。
$('#contact-form input[name="subject"]')[0].oninvalid = function(e) {
if (!e.target.validity.valid) {
e.target.setCustomValidity(t('Please fill out this field!'));
}
};
$('#contact-form input[name="subject"]')[0].oninput = function(e) {
e.target.setCustomValidity('');
};
$('#contact-form input[name="phone_or_mail"]')[0].oninvalid = function(e) {
if (!e.target.validity.valid) {
e.target.setCustomValidity(t('Please fill out this field!'));
}
};
$('#contact-form input[name="phone_or_mail"]')[0].oninput = function(e) {
e.target.setCustomValidity('');
};
$('#contact-form textarea[name="question"]')[0].oninvalid = function(e) {
if (!e.target.validity.valid) {
e.target.setCustomValidity(t('Please fill out this field!'));
}
};
$('#contact-form textarea[name="question"]')[0].oninput = function(e) {
e.target.setCustomValidity('');
};
t() 是我的翻译函数。整个问题是导致此问题的原因,以获取已翻译的错误消息。
IE 10- 在提交事件上有验证回退。
$('#contact-form').submit(function(e) {
e.preventDefault();
var subject = $('input[name="subject"]');
//if empty or value left as placeholder
if (subject.val() == '' || subject.val() == t('Subject')) {
subject.css('border', '2px solid red');
return false;
}
var phone = $('input[name="phone_or_mail"]');
if (phone.val() == '' || subject.val() == t('Phone number or E-mail address')) {
phone.css('border', '2px solid red');
return false;
}
var q = $('textarea[name="question"]');
if (q.val() == '' || subject.val() == t('Your Question here ...')) {
q.css('border', '2px solid red');
return false;
}
...
$.ajax