必填校验加变色,点击颜色消失
发布日期:2022-04-11 11:39:18 浏览次数:33 分类:技术文章

本文共 1351 字,大约阅读时间需要 4 分钟。

必填校验加变色,点击颜色消失

1.例子

     

通用的必填校验工具方法

//去掉空格function trim(value) {
if (value) {
value = (value.toString()).replace(/^\s*|\s*$/g, ""); } else {
return ""; } return value;}
//必填校验function verification() {
var msg = []; $('.noNull').each(function () {
var name = $(this).attr("name"); if ($(this).attr("type") == "radio" && $("input[name=" + name + "]:checked").size() < 1) {
msg.push(trim($(this).attr('notNull'))); $('[changeColorForNull=' + $(this).attr('id') + ']').css('background', 'yellow'); } else if ($(this).attr("type") == "checkbox" && $("input[name=" + name + "]:checked").size() < 1) {
msg.push(trim($(this).attr('notNull'))); $('[changeColorForNull=' + $(this).attr('id') + ']').css('background', 'yellow'); } else if (!trim($(this).val())) {
msg.push(trim($(this).attr('notNull'))); $('[changeColorForNull=' + $(this).attr('id') + ']').css('background', 'yellow'); } }); return msg;}

changeColorForNull是自定义的,主要就是通过id获取值去指向父元素自定义的属性来实现变色。

//点击移除必填颜色function myFocus(obj) {
//将样式消除 $('[changeColorForNull=' + $(obj).attr("id") + ']').attr('style', '')}

2.说明

.parent(),虽然也能指向父元素,但是如果改变样式,多套了一层div,那就会出现变样了,推荐使用自定义属性,通过id去获取值去指向自定义的属性。
onfocus 事件在对象获得焦点时发生,比如点击输入框。

转载地址:https://blog.csdn.net/Uluoyu/article/details/119411373 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:必填项前的红色星号
下一篇:必刷算法题必刷算法题leetcode

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月10日 19时08分16秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章