展会信息港展会大全

jquery.form.js用法之清空form的方法
来源:互联网   发布日期:2016-01-27 15:38:57   浏览:1675次  

导读:这篇文章主要介绍了jquery form js清空form的方法,需要的朋友可以参考下本段代码摘取自jquery form js中,由于觉得该方法的使用性非常强,同时也可独立拿出来使用。该段代码言简意赅可以很好的作为学习参考。代码 ...

这篇文章主要介绍了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 = '';

}

}

});

};

赞助本站

人工智能实验室

相关热词: form jquery js

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港