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

首頁 > 語言 > JavaScript > 正文

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

2024-05-06 16:02:22
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了jquery.form.js清空form的方法,需要的朋友可以參考下

本段代碼摘取自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 = '';
            }
        }
    });
};

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 吉林省| 湘乡市| 永清县| 禹州市| 平邑县| 广南县| 增城市| 斗六市| 吉林市| 吴桥县| 湘阴县| 新化县| 华池县| 周至县| 边坝县| 扶沟县| 芮城县| 双柏县| 拜泉县| 永德县| 开封市| 浪卡子县| 平安县| 珠海市| 北碚区| 朔州市| 池州市| 济南市| 陆河县| 星子县| 弋阳县| 新沂市| 怀集县| 通河县| 邢台市| 芒康县| 靖远县| 德钦县| 明水县| 郁南县| 溆浦县|