【发布时间】:2019-03-18 16:41:40
【问题描述】:
我在表单上使用这些自定义 CSS 复选框,但注意到在我提交表单(然后运行 jQuery 验证)后,自定义复选框不再显示。复选框行为仍然“有效”,只是复选框不再以自定义方式设置样式。
我整理了一个小提琴来展示这个:https://jsfiddle.net/gamehendgeVA/z31q59hc/8/
另外,我将在此处包含代码。
您可以看到,在默认情况下,选中和取消选中复选框都可以正常工作。在您提交表单的那一刻,复选框不再设置样式(但是,它仍然“有效”,只是不再带有自定义样式复选标记的蓝色背景)。
任何关于如何最好地“解决”这个问题的建议都很棒。
谢谢!
HTML
<form id="addressForm" name="addressForm" method="post">
<div class="row">
<div class="col-md-12">
<div class="alertBox infoAlert" style="padding-top:8px;padding-left:4px;">
<div class="checkbox-inline">
<label class="checkboxContainer">I have read and agree to the Privacy Policy
<input type="checkbox" name="privacyPolicyCheckbox" id="privacyPolicyCheckbox" value="no">
<span class="checkmark"></span>
</label>
</div>
</div><!-- /.alertBox -->
</div>
</div>
<div style="margin-top:20px;"><input type="submit" value="Go to Next Screen" class="btn btn-cta-ecommerce fullWidthButton760" id="addressFormSubmit" style="min-width:350px;" /></div>
</form>
CSS
/* Customize the label (the container) */
.checkboxContainer {
display: block;
position: relative;
padding-left: 35px;
/* margin-bottom: 12px; */
cursor: pointer;
font-size: 18px;/* was 22px */
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox */
.checkboxContainer input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 13px;/* was 0 */
left: 0;
height: 25px;
width: 25px;
background-color: #fff;/* was #eee */
border: 1px solid #333;/* added */
}
/* On mouse-over, add a grey background color */
.checkboxContainer:hover input ~ .checkmark {
background-color: #d1d1d1;/* was #ccc */
}
/* When the checkbox is checked, add a blue background */
.checkboxContainer input:checked ~ .checkmark {
background-color: #2196F3;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.checkboxContainer input:checked ~ .checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.checkboxContainer .checkmark:after {
left: 10px;
top: 6px;
width: 6px;
height: 11px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
JS
jQuery.validator.setDefaults({
errorElement: "span",
errorClass: "help-block",
highlight: function(element) {
jQuery(element).parent().is('.has-success, .has-error') ?
jQuery(element).parent().removeClass('has-success').addClass('has-error') :
jQuery(element).wrap('<span class="has-error"></span>');
},
unhighlight: function(element) {
jQuery(element).parent().is('.has-success, .has-error') ?
jQuery(element).parent().removeClass('has-error').addClass('has-success') :
jQuery(element).wrap('<span class="has-success"></span>');
},
errorPlacement: function(error, element) {
if (element.attr("name") == "nonMemberType") //not needed on this screen, but keeping placeholder syntax
error.insertAfter("#selectNonMemberValidationBlock"); //not needed, see above
else {
error.insertAfter(element);
}
}
});
jQuery("#addressForm").validate({
rules: {
'privacyPolicyCheckbox': {
required: true
},
},
messages: {
'privacyPolicyCheckbox': {
required: "Please indicate that you agree to the Terms and Conditions by checking this box."
}
}
});
【问题讨论】:
-
我无法在 jsfiddle 上重现它...
-
嗨@Alessio - 我不知道为什么不...基本上,复选框首先选中和未选中。然后,按下提交按钮。然后,复选框不再设置样式(选中)。它仍然会切换 jQuery 验证消息(显示它正在添加/删除 .has-error 和 .has-success 代码),但是,在启动 jQuery 验证后复选框不再样式化。
-
嗨@gamehendge,问题是当我按下按钮时,表单被发送并且JSfiddle或Codepen有点崩溃......所以我看不到损坏的复选框......
-
嗨@Alessio - 我明白你的意思,但表单并不总是在jFiddle中提交......你仍然可以触发阻止它提交的jQuery验证,然后查看行为...表单上没有“操作”,我尝试将其更改为获取而不是发布,但这不起作用(您知道如何更改表单代码以不允许 404 页面吗? )。但是同样,如果您先选中,然后取消选中,然后提交,那么您将“卡住”表单验证工作,这将阻止提交,并且您可以看到“不再有复选框样式”问题,至少与FF
-
绝对没用的jsFiddle。我已禁用通过
submitHandler提交,因此您可以看到这里甚至没有任何内容被验证。 jsfiddle.net/ykhn5stv
标签: jquery css twitter-bootstrap checkbox jquery-validate