国产探花免费观看_亚洲丰满少妇自慰呻吟_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 = '';
            }
        }
    });
};

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

圖片精選

主站蜘蛛池模板: 满城县| 景泰县| 武义县| 浏阳市| 大英县| 盐山县| 深州市| 新乡县| 麦盖提县| 布拖县| 盖州市| 连江县| 奉新县| 萨嘎县| 荥阳市| 蒙阴县| 通许县| 隆德县| 达尔| 绵竹市| 桃源县| 漳州市| 察哈| 开鲁县| 仪陇县| 屏东县| 禹州市| 永靖县| 五寨县| 沁水县| 大城县| 高淳县| 图们市| 沂南县| 长汀县| 敖汉旗| 上饶市| 徐闻县| 达尔| 丹棱县| 登封市|