未使用jQuery验证器选中Css样式复选框时
Css style checkbox when not checked with jQuery validator
如果在单击提交按钮后未选中复选框,我希望我的复选框更改标签颜色。我使用的是jQuery验证器。我不想标签显示,我只想把"我接受"更改为红色。
验证
jQuery.extend(jQuery.validator.messages, {
required:"This field is required",
email: "A valid email address is required"
});
复选框
<label for="cbx2">
<input name="TermsCondition" type="checkbox" value="on" style="float:left;" class="required" id="cbx2">
<strong>I Accept</strong>
</label>
Css
label.error {
font: 0/0 a;
}
以下是使用css选择器修复此问题的最简单的css破解方法。只需将其添加到css中即可。
.checkbox.valid ~ strong {
color:#000!important;
}
input.error, .checkbox.error ~ strong{
border: 1px dotted red!important;
color:red!important;
}
label.error {
display: none!important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.js"></script>
<script>
$.validator.setDefaults({
submitHandler: function() {
alert("submitted!");
}
});
$().ready(function() {
// validate signup form on keyup and submit
$("#signupForm").validate({
rules: {
firstname: "required",
lastname: "required",
username: {
required: true,
minlength: 2
},
password: {
required: true,
minlength: 5
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
},
email: {
required: true,
email: true
},
topic: {
required: "#newsletter:checked",
minlength: 2
},
agree: "required"
},
messages: {
firstname: "Please enter your firstname",
lastname: "Please enter your lastname",
username: {
required: "Please enter a username",
minlength: "Your username must consist of at least 2 characters"
},
password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
},
confirm_password: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long",
equalTo: "Please enter the same password as above"
},
email: "Please enter a valid email address",
agree: "Please accept our policy",
topic: "Please select at least 2 topics"
}
});
// propose username by combining first- and lastname
$("#username").focus(function() {
var firstname = $("#firstname").val();
var lastname = $("#lastname").val();
if (firstname && lastname && !this.value) {
this.value = firstname + "." + lastname;
}
});
});
</script>
<div id="main">
<form class="cmxform" id="signupForm" method="get" action="">
<fieldset>
<legend>Validate a complete form</legend>
<p>
<label for="firstname">Firstname</label>
<input id="firstname" name="firstname" type="text">
</p>
<p>
<label for="lastname">Lastname</label>
<input id="lastname" name="lastname" type="text">
</p>
<p>
<label for="username">Username</label>
<input id="username" name="username" type="text">
</p>
<p>
<label for="password">Password</label>
<input id="password" name="password" type="password">
</p>
<p>
<label for="confirm_password">Confirm password</label>
<input id="confirm_password" name="confirm_password" type="password">
</p>
<p>
<label for="email">Email</label>
<input id="email" name="email" type="email">
</p>
<p>
<label for="agree">Please agree to our policy</label>
<input type="checkbox" class="checkbox" id="agree" name="agree">
<strong>I Accept</strong>
</p>
<p>
<input class="submit" type="submit" value="Submit">
</p>
</fieldset>
</form>
您可以使用Jquery validate回调函数(成功时,显示错误时):
$("#registerForm").validate({
success: function() {
$("#L_Accept").css('color', 'black');
},
showErrors: function() {
$("#L_Accept").css('color', 'red');
this.defaultShowErrors();
},
/* Disable (focusout, keyup, click) Events
to force check validation by Submit Button ONLY.
*/
onfocusout: false,
onkeyup: false,
onclick: false
});
label.error {
display:none !important; //hide default error label.
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<h1>Form Validation Example</h1>
<form id='registerForm' name='registerForm' method='post' action='' >
<label for="cbx2">
<input name="TermsCondition" type="checkbox" value="on" style="float:left;" class="required" id="cbx2">
<strong id="L_Accept">I Accept</strong>
</label>
<input type='submit' name='Submit' value='Submit' />
</form>
您可以将css
:not()
与参数:checked
一起使用,相邻的同级+
选择器;在onsubmit
事件中,如果#cbx2
不是checked
,则添加将#cbx2
color
设置为red
的className
document.getElementById("form").onsubmit = function(event) {
var message = this.querySelector("#cbx2");
if (!message.checked) message.className = "invalid";
}
#cbx2:not(:checked).invalid + strong {
color: red;
}
<form id="form">
<label for="cbx2">
<input name="TermsCondition" type="checkbox" value="on" style="float:left;" class="required" id="cbx2">
<strong>I Accept</strong>
</label>
<input type="submit" />
</form>
相关文章:
- 如果文本字段为空,则使用JavaScript应用CSS样式
- FF和Chromium中CSS样式按钮的外观差异
- CSS样式属性留空
- CKeditor预览插件.js,为Chrome和IE进行编辑.因为它拒绝CSS样式
- 基于路由的CSS样式-Angular2
- 为未标记的文本添加CSS样式
- 使用JQuery将现有CSS样式扩展%-%
- JavaScript-在keyDown上切换css样式
- React js应用程序's在托管应用程序中打开时将覆盖css样式
- 如何在javascript中更改段落中不同行的css样式
- 通过javascript设置IE特定的css样式
- PHP,MySQL,AJAX-调用AJAX结果后,Bootstrap CSS样式不会显示
- 使用javascript/jquery检查.css样式表的名称
- JavaScript和CSS样式不适用于我的“;谷歌网站”;页
- 未使用jQuery验证器选中Css样式复选框时
- javascript正则表达式,用于编辑元素内部的css样式属性
- css样式加载+id.例如加载1、加载300
- 无法在运行时将CSS样式添加到元素中
- d3js 通过其 CSS 样式选择所有元素
- 如何根据浏览器宽度动态调整 css 样式表