我想清除表单中的所有输入和文本区域字段。当使用reset类的输入按钮时,它的工作原理如下:
$(".reset").bind("click", function() {
$("input[type=text], textarea").val("");
});
这将清除页面上的所有字段,而不仅仅是表单中的字段。我的选择器看起来像什么形式的实际重置按钮生活?
我想清除表单中的所有输入和文本区域字段。当使用reset类的输入按钮时,它的工作原理如下:
$(".reset").bind("click", function() {
$("input[type=text], textarea").val("");
});
这将清除页面上的所有字段,而不仅仅是表单中的字段。我的选择器看起来像什么形式的实际重置按钮生活?
当前回答
当页面包含一个涉及IHttpHandler请求处理(captcha)的web用户控件调用时,以上任何一种方法都不适用。 在发送请求(用于图像处理)后,下面的代码不清除表单上的字段(在发送HttpHandler请求之前),一切都正常工作。
<input type="reset" value="ClearAllFields" onclick="ClearContact()" />
<script type="text/javascript">
function ClearContact() {
("form :text").val("");
}
</script>
其他回答
对于jQuery 1.6+:
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.prop('checked', false)
.prop('selected', false);
对于jQuery < 1.6:
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
请看这篇文章: 使用jQuery重置多级表单
Or
$('#myform')[0].reset();
jQuery建议:
要检索和更改DOM属性,例如表单元素的选中、选中或禁用状态,请使用.prop()方法。
如果你想清空所有的输入框,不管它的类型是什么,那么这是一分钟的步骤
$('#MyFormId')[0].reset();
$(".reset").click(function() {
$(this).closest('form').find("input[type=text], textarea").val("");
});
有什么理由不应该使用它吗?
$("#form").trigger('reset');
我写了一个通用的jQuery插件:
/** * Resets any input field or form */ $.fn.uReset = function () { return this.filter('form, :input').each(function () { var input = $(this); // Reset the form. if (input.is('form')) { input[0].reset(); return; } // Reset any form field. if (input.is(':radio, :checkbox')) { input.prop('checked', this.defaultChecked); } else if (input.is('select')) { input.find('option').each(function () { $(this).prop('selected', this.defaultSelected); }); } else if (this.defaultValue) { input.val(this.defaultValue); } else { console.log('Cannot reset to default value'); } }); }; $(function () { // Test jQuery plugin. $('button').click(function (e) { e.preventDefault(); var button = $(this), inputType = button.val(), form = button.closest('form'); if (inputType === 'form') { form.uReset() } else { $('input[type=' + inputType + '], ' + inputType, form).uReset(); } }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <h3>Form</h3> <form> <input type="text" value="default"/><br /><br /> Ch 1 (default checked) <input type="checkbox" name="color" value="1" checked="checked" /><br /> Ch 2 <input type="checkbox" name="color" value="2" /><br /> Ch 3 (default checked) <input type="checkbox" name="color" value="3" checked="checked" /><br /><br /> <select name="time"><br /> <option value="15">15</option> <option selected="selected" value="30">30</option> <option value="45">45</option> </select><br /><br /> R 1 <input type="radio" name="color" value="1" /><br /> R 2 (default checked) <input type="radio" name="color" value="2" checked="checked" /><br /> R 3 <input type="radio" name="color" value="3" /><br /><br /> <textarea>Default text</textarea><br /><br /> <p>Play with form values and then try to reset them</p> <button type="button" value="text">Reset text input</button> <button type="button" value="checkbox">Reset checkboxes</button> <button type="button" value="select">Reset select</button> <button type="button" value="radio">Reset radios</button> <button type="button" value="textarea">Reset textarea</button> <button type="button" value="form">Reset the Form</button> </form>