Javascript 清除除某些html控件之外的所有html控件

Javascript 清除除某些html控件之外的所有html控件,javascript,jquery,html,Javascript,Jquery,Html,我有一个表单,它由文本框、文本区、密码和一些剑道下拉列表和组合框组成。我想清除clear按钮单击上的所有控件,除了一个文本框和文本区域 我已完成以下操作以清除所有控件 $('#btnClear').on('click', function () { $(this).closest('form').find('input[type=text], textarea, input[type=password]').val(''); }); 我不知道怎样才能不清除一些控制 试着像这样使用 让som

我有一个表单,它由文本框、文本区、密码和一些剑道下拉列表和组合框组成。我想清除
clear
按钮单击上的所有控件,除了一个文本框和文本区域

我已完成以下操作以清除所有控件

$('#btnClear').on('click', function () {
  $(this).closest('form').find('input[type=text], textarea, input[type=password]').val('');
});
我不知道怎样才能不清除一些控制

试着像这样使用

someId
是一个文本框,
otherId
是不应该为空的texxarea

$('#btnClear').on('click', function () {
    $(this).closest('form').find('input:not(#someId),textarea:not(#otherId)').val('');
});
您还可以使用一个公共类,如
非空元素
,与
:not
类似

$('#btnClear').on('click', function () {
    $(this).closest('form')
           .find('input[type=text], textarea, input[type=password]')
           .not('.non-empty-elements')// filter non-empty-elements
           .val('');
});
请注意,您需要将
非空元素
类添加到那些不应为空的元素中

使用过滤方法:

$('#btnClear').on('click', function () {
    $(this).closest('form').find('input:not(#someId),textarea:not(#otherId)').val('');
});
描述:从匹配的元素集中删除元素


其中
foo
是一个引用异常(即您不想清除的控件)的选择器。

您可以通过以下方法实现该功能:

将某些类应用于不希望清除的控件,如下所示:

<div id="parent"> 
    <input type="text" id="textOne" /><br/>
    <input type="text" id="textTwo" class="ignoreT" /><br/>
    <textarea id="textThree" class="ignoreTA" ></textarea><br/>
    <input type="text" id="textFour" class="ignoreT" /><br/>
    <button id="clear">Clear</button>
</div>
或者,您可以对所有控件应用相同的类(假设忽略),并编写以下代码:

$('#clear').click(function(){
   $(this).closest('form').find('input, textarea').not('.ignore').val('');
});
看看它是否适合您。

使用

其中
#myControlId1
#myControlId1
是您不想清除的控件的ID,您可以使用和定义这些控件,但类似的控件除外

$(html).not('span:first').remove();
参考链接: 1.

二,


基于css:-

发布您的html并指定您不想清除的内容。not('yadayada)您可以发布html吗?使用.not()像
$(this).closest('form')。find('input[type=text],textarea,input[type=password]')。not('').val('');
谢谢这对我有用,也谢谢您用适当的例子进行了很好的解释
$(this).closest('form').find('input[type=text], textarea, input[type=password]').not('#myControlId1,#myControlId2').val('');
$(html).not('span:first').remove();