Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23291
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 134
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 15:58
文章分类

全部博文(17)

文章存档

2014年(17)

我的朋友

分类: jQuery

2014-05-26 18:25:45

jquery.form.js中的清空表单功能
//取自jquery.form.js插件中的清空表单功能理解,提取出来可在需要的时候独立使用。
/**
* 将指定的form表单上的input、select、textarea的数据清除,隐藏框作为参数可选输入true/false或者是样式属性
*/
$.fn.clearForm = function(includeHidden) {
    return this.each(function() {
        $('input,select,textarea', this).clearFields(includeHidden);    //this表示设置上下文环境,有多个表单时只作用指定的表单
    });
};

$.fn.clearFields = $.fn.clearInputs = function(includeHidden) {
    var re = /^(?:color|date|datetime|email|month|number|password|range|search|tel|text|time|url|week)$/i; // 正则表达式匹配type属性
    return this.each(function() {
        var t = this.type, tag = this.tagName.toLowerCase(); //获取元素的type属性和标签
        if (re.test(t) || tag == 'textarea') {
            this.value = '';
        }
        else if (t == 'checkbox' || t == 'radio') {
            this.checked = false;
        }
        else if (tag == 'select') {
            this.selectedIndex = -1;
        }
        else if (t == "file") {
            if (/MSIE/.test(navigator.userAgent)) {
                $(this).replaceWith($(this).clone(true));
            } else {  
                $(this).val('');
            }
       }
       else if (includeHidden) {
            if ( (includeHidden === true && /hidden/.test(t)) ||
                 (typeof includeHidden == 'string' && $(this).is(includeHidden)) ) {   //true 、false或者样式属性
                this.value = '';
            }
        }
    });
};
阅读(519) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~