jquery - 在使用 jquery validate 时,我应该对这个特定的 css 做些什么更改以防止错误?

标签 jquery css forms jquery-validate input-field

我有一个基本注册表。为了设置输入字段的样式,我的 css 代码在下面,但我使用的代码来自 codrops 的演示:http://tympanus.net/codrops/2015/01/08/inspiration-text-input-effects/

我特别使用第二个示例(称为 Hoshi)。现在,当您单击输入字段时,会出现一个蓝色的底部边框。

我正在使用 Jquery 验证插件,所以我的 js 脚本代码如下。我在更改错误的 CSS 时遇到问题。例如,当用户忘记填写字段但单击提交时,我希望输入字段的边框底部为红色,直到其通过验证。但我完全不知道如何实现这一目标。任何见解表示赞赏!谢谢!

$(document).ready(function(){
	$('#tbi-form').validate({
		rules: {
			First_Name: {
				required: true,
				maxlength: 50
			},
			Last_Name: {
				required: true
			},
			Phone: {
				required: true
			},
			zip: {
				required: true,
				maxlength: 20,
				digits: true
			},
			birth_month: {
				required: true
			},
			Email: {
				required: true,
				maxlength: 100
			},
			Email_Confirm: {
				required: true,
				equalTo: "#Email"
			},
			Referral: {
				required: true,
				maxlength: 20
			}
		},
		messages: {
			First_Name: "Please enter your first name",
			Last_Name: "Please enter your last name",
			Email: "Please enter your Email",
			Email_Confirm: {
				required: "Please enter your email",
				equalTo: "Please make sure the email you entered is accurate"
			}
		},
		errorElement: 'div'
	});
});
.input__field--form {
	margin-top: 1em;
	padding: 1.5em 0em 1.2em 0.2em;
	width: 100%; 
	background: transparent;
	color: #595F6E;
	font-size: 85.25%;
}

.input__label--form {
	position: absolute;
	bottom: 0;
	left: 0;
	padding: 0 0.25em;
	width: 100%;
	height: calc(100% - 1em);
	text-align: left;
	pointer-events: none;
}

.input__label-content--form {
	position: absolute;
}

.input__label--form::before,
.input__label--form::after {
	content: '';
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: calc(100% - 5px);
	border-bottom: 1px solid #B9C1CA;
}

.input__label--form::after {
	margin-top: 0px;
	border-bottom: 4px solid red;
	-webkit-transform: translate3d(-100%, 0, 0);
	transform: translate3d(-100%, 0, 0);
	-webkit-transition: -webkit-transform 0.3s;
	transition: transform 0.3s;
}

.input__label--form-color-1::after {
	border-color: #00A0E2;
}

.input__field--form:focus + .input__label--form::after,
.input--filled .input__label--form::after {
	-webkit-transform: translate3d(0, 0, 0);
	transform: translate3d(0, 0, 0);
}

.input__field--form:focus + .input__label--form .input__label-content--form,
.input--filled .input__label-content--form {
	-webkit-animation: anim-1 0.3s forwards;
	animation: anim-1 0.3s forwards;
}

@-webkit-keyframes anim-1 {
	50% {
		opacity: 0;
		-webkit-transform: translate3d(1em, 0, 0);
		transform: translate3d(1em, 0, 0);
	}
	51% {
		opacity: 0;
		-webkit-transform: translate3d(-1em, -40%, 0);
		transform: translate3d(-1em, -40%, 0);
	}
	100% {
		opacity: 1;
		-webkit-transform: translate3d(0, -40%, 0);
		transform: translate3d(0, -40%, 0);
	}
}

@keyframes anim-1 {
	50% {
		opacity: 0;
		-webkit-transform: translate3d(1em, 0, 0);
		transform: translate3d(1em, 0, 0);
	}
	51% {
		opacity: 0;
		-webkit-transform: translate3d(-1em, -40%, 0);
		transform: translate3d(-1em, -40%, 0);
	}
	100% {
		opacity: 1;
		-webkit-transform: translate3d(0, -40%, 0);
		transform: translate3d(0, -40%, 0);
	}
}

最佳答案

引用the documentation .您有两个选项,称为 errorClass and validClass分别默认为 errorvalid。您可以将这些更改为您需要的任何 CSS 类名称...

$(document).ready(function(){
    $('#tbi-form').validate({
        errorClass: "myClass",
        validClass: "myOtherClass",
        errorElement: 'div',
        rules: {
            First_Name: {
                .....

插件将这些类应用于错误消息和输入元素。因此,在您的 CSS 中,您必须分别针对这两种事物...

input[type="text"].myClass {
    /* CSS properties applied to text inputs on error */
}

input[type="text"].myOtherClass {
    /* CSS properties applied to text inputs when valid */
    /* may not be needed since the validated elements usually look the same as before */
}

由于您的错误消息标签已更改为 div 元素...

div.myClass {
    /* CSS properties applied to error label on error */
}

div.myOtherClass {
    /* CSS properties applied to error label when valid */
    /* probably not needed since you don't show an error label when valid */
}

关于jquery - 在使用 jquery validate 时,我应该对这个特定的 css 做些什么更改以防止错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31372004/

相关文章:

jquery - 如何在同一行制作两个按钮?

html - float CSS后Div崩溃

javascript - 在 JavaScript 中验证后提交表单的确认消息

html - 如何明确标注屏幕阅读器的数字输入格式?

jquery - jqgrid不传递记录id进行编辑删除

javascript - JQuery 与 extend (reduce objects) 相反

javascript - 单击另一个 DIV 重新加载一个 DIV

javascript - [jQuery]如何从 'loadedmetadata'获取视频的宽度?

css - 为什么按钮元素不受CSS影响?

javascript - jQuery UI Datepicker 的问题