必填校验加变色,点击颜色消失
1.例子
<td changeColorForNull="ah0"><input class="noNull" type="text" id="ah0"name="ah" notNull="案号" onfocus="myFocus(this)" value="" />
</td>
通用的必填校验工具方法
//去掉空格
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 事件在对象获得焦点时发生,比如点击输入框。