国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

jquery.form.js用法之清空form的方法

2019-11-20 20:58:32
字體:
來源:轉載
供稿:網友

本段代碼摘取自jquery.form.js中,由于覺得該方法的使用性非常強,同時也可獨立拿出來使用。
該段代碼言簡意賅可以很好的作為學習參考。

復制代碼 代碼如下:

/**
 * Clears the form data. Takes the following actions on the form's input fields:
 * - input text fields will have their 'value' property set to the empty string
 * - select elements will have their 'selectedIndex' property set to -1
 * - checkbox and radio inputs will have their 'checked' property set to false
 * - inputs of type submit, button, reset, and hidden will *not* be effected
 * - button elements will *not* be effected
 */
$.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; // 'hidden' is not in this list
    return this.each(function() {
        var t = this.type, tag = this.tagName.toLowerCase();
        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) {
            // includeHidden can be the value true, or it can be a selector string
            // indicating a special test; for example:
            // $('#myForm').clearForm('.special:hidden')
            // the above would clean hidden inputs that have the class of 'special'
            if ( (includeHidden === true && /hidden/.test(t)) ||
                 (typeof includeHidden == 'string' && $(this).is(includeHidden)) ) {
                this.value = '';
            }
        }
    });
};

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苗栗县| 珠海市| 金寨县| 醴陵市| 仙居县| 许昌县| 麦盖提县| 宝兴县| 牡丹江市| 娱乐| 清水县| 满城县| 楚雄市| 都昌县| 大英县| 镇沅| 哈密市| 海兴县| 内丘县| 米林县| 韶关市| 辛集市| 镇安县| 漳浦县| 且末县| 嘉峪关市| 津市市| 朝阳县| 平江县| 清新县| 兰州市| 甘孜县| 资讯 | 台南县| 新安县| 阳东县| 城市| 监利县| 安远县| 安乡县| 综艺|