我似乎无法在页面加载时可靠地添加这些事件侦听器。一些听众从一开始就工作,但其他人只在点击几下后工作。可能有什么问题?问题可能是我将本机 JS 与 jQuery 混合在一起吗?
<script>
$(document).ready(function() {
function resetValidation() {
jQuery('#contact-form').validate().resetForm();
};
function resetFormFields() {
document.getElementById('contact-form').reset();
};
function removeValidationClasses() {
jQuery('.control-group').removeClass('success').removeClass('error');
};
function changeHeaderToAdd() {
document.getElementById('contactModalLabel').innerText = 'Add Contact';
};
function changeHeaderToUpdate() {
document.getElementById('contactModalLabel').innerText = 'Update Contact';
};
function clearContactId() {
document.getElementById('contactId').value = '';
};
var ab = document.getElementById('addContactBtn');
if (ab.addEventListener) {
ab.addEventListener('click', resetValidation, false);
ab.addEventListener('click', resetFormFields, false);
ab.addEventListener('click', removeValidationClasses, false);
ab.addEventListener('click', changeHeaderToAdd, false);
ab.addEventListener('click', clearContactId, false);
} else {
ab.attachEvent('onclick', resetValidation);
ab.attachEvent('onclick', resetFormFields);
ab.attachEvent('onclick', removeValidationClasses);
ab.attachEvent('onclick', changeHeaderToAdd);
ab.attachEvent('onclick', clearContactId);
};
var ub = document.getElementById('updateContactBtn');
if (ub.addEventListener) {
ub.addEventListener('click', resetValidation, false);
ub.addEventListener('click', resetFormFields, false);
ub.addEventListener('click', removeValidationClasses, false);
ub.addEventListener('click', changeHeaderToUpdate, false);
} else {
ub.attachEvent('onclick', resetValidation);
ub.attachEvent('onclick', resetFormFields);
ub.attachEvent('onclick', removeValidationClasses);
ub.attachEvent('onclick', changeHeaderToUpdate);
};
});
</script>