必填校验加变色,点击颜色消失
生活随笔
收集整理的這篇文章主要介紹了
必填校验加变色,点击颜色消失
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
必填校驗加變色,點擊顏色消失
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獲取值去指向父元素自定義的屬性來實現(xiàn)變色。
//點擊移除必填顏色 function myFocus(obj) {//將樣式消除$('[changeColorForNull=' + $(obj).attr("id") + ']').attr('style', '') }2.說明
.parent(),雖然也能指向父元素,但是如果改變樣式,多套了一層div,那就會出現(xiàn)變樣了,推薦使用自定義屬性,通過id去獲取值去指向自定義的屬性。
onfocus 事件在對象獲得焦點時發(fā)生,比如點擊輸入框。
總結
以上是生活随笔為你收集整理的必填校验加变色,点击颜色消失的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 输入框不可以输入中文
- 下一篇: plupload使用例子