我是编码新手,我想知道如何删除此代码中所需的字段。非常感谢。例如,当字段为空时,我可以提交而不会显示任何错误。
> function valid_inf() { var errorUsername = document.getElementById('first_name-error'); var usernameInput =
> document.getElementById('first_name'); var errphone =
> document.getElementById('phone-error'); var phoneInput =
> document.getElementById('phone'); var erroremail =
> document.getElementById('email-error'); var emailInput =
> document.getElementById('email'); var firstname =
> $('#first_name').val(); var phone = $('#phone').val(); var email =
> $('#email').val(); if (firstname == "") {
> errorUsername.style.display = 'block';
> usernameInput.className = 'form__input form__input--red rounded-4';
>
> }else{
> $("#first_name").on('keyup', function(){
> errorUsername.style.display = 'none';
> usernameInput.className = 'form__input rounded-4'; });
>
> } if (phone == "") {
> errphone.style.display = 'block';
> phoneInput.className = 'form__input form__input--red rounded-4';
>
> }else{
> $("#phone").on('keyup', function(){
> errphone.style.display = 'none';
> phoneInput.className = 'form__input rounded-4'; });
>
> } if (email == "") {
> erroremail.style.display = 'block';
> emailInput.className = 'form__input form__input--red rounded-4';
> return false; }else{
> $("#email").on('keyup', function(){
> erroremail.style.display = 'none';
> emailInput.className = 'form__input rounded-4'; });
如果要删除验证,只需删除特定字段名的if-else条件
例如,如果您想删除first_name验证,则需要像以下一样更改代码
function valid_inf() {
var errorUsername = document.getElementById('first_name-error');
var usernameInput =
document.getElementById('first_name');
var errphone =
document.getElementById('phone-error');
var phoneInput =
document.getElementById('phone');
var erroremail =
document.getElementById('email-error');
var emailInput =
document.getElementById('email');
var firstname =
$('#first_name').val();
var phone = $('#phone').val();
var email =
$('#email').val();
// if (firstname == "") {
// errorUsername.style.display = 'block';
// usernameInput.className = 'form__input form__input--red rounded-4';
// } else {
// $("#first_name").on('keyup', function () {
// errorUsername.style.display = 'none';
// usernameInput.className = 'form__input rounded-4';
// });
// }
if (phone == "") {
errphone.style.display = 'block';
phoneInput.className = 'form__input form__input--red rounded-4';
} else {
$("#phone").on('keyup', function () {
errphone.style.display = 'none';
phoneInput.className = 'form__input rounded-4';
});
}
if (email == "") {
erroremail.style.display = 'block';
emailInput.className = 'form__input form__input--red rounded-4';
return false;
} else {
$("#email").on('keyup', function () {
erroremail.style.display = 'none';
emailInput.className = 'form__input rounded-4';
});