相当有用的jQuery函数,用于清除我在Karl Swedberg网站上找到的所有表格数据。 它只是简单地从表单中删除所有数据,包括文本输入,选择框,单选按钮,复选框等…有两个版本,第二个版本可能更有用,因为您可以将它作为jQuery函数直接应用于DOM元素。
function clearForm(form) {
// iterate over all of the inputs for the form
// element that was passed in
$(':input', form).each(function() {
var type = this.type;
var tag = this.tagName.toLowerCase(); // normalize case
// it's ok to reset the value attr of text inputs,
// password inputs, and textareas
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = "";
// checkboxes and radios need to have their checked state cleared
// but should *not* have their 'value' changed
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
// select elements need to have their 'selectedIndex' property set to -1
// (this works for both single and multiple select elements)
else if (tag == 'select')
this.selectedIndex = -1;
});
};
重置输入按钮
您可以放置一个类型为reset的隐藏输入,然后触发它以清除表单。
$('form > input[type=reset]').trigger('click'); //with a reset button in the form set to display: none;
jQuery元素功能
$.fn.clearForm = function() {
return this.each(function() {
var type = this.type, tag = this.tagName.toLowerCase();
if (tag == 'form')
return $(':input',this).clearForm();
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = '';
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
else if (tag == 'select')
this.selectedIndex = -1;
});
};
//usage
$('#flightsSearchForm').clearForm();
From: https://www.sitepoint.com/jquery-function-clear-form-data/