删除具有特定值的复选框

时间:2010-09-09 00:00:46

标签: javascript jquery

我有以下代码片段,我无权访问标记以添加id / classes。

我需要做的是删除任何具有特定值的复选框。有大约10个值,如果任何复选框具有这些值,则需要从标记中删除它们。除此之外,我还需要在此复选框输入之前的同一时间删除其关联的文本“添加”。我如何仅针对这些复选框输入和关联的“添加”文本进行删除?

编辑:让我们说值是1234,abcd,efgh,ijkl,54330,ll64等......我需要检查这些和其他值。

<table width="100%"cellpadding="0" cellspacing="0"><tr>
<td valign="top">
<b><FONT class="pricecolorsmall colors_productprice">Sale Price: £4.95 </font></b>
<br /><div class="337">Add</div> <input type="checkbox" name="ProductCode2"
value="480GCFD"> 
</td>
</tr></table> 
</td> 
<td valign="top" width="25%"> 
<table width="100%"cellpadding="0" cellspacing="0"><tr>
<td valign="top">
<b><font class="pricecolorsmall colors_productprice"><font class="smalltext colors_text"><b>Sale Price </b></font> £1.00 </font></b>

<br /><div class="337">Add</div> <input type="checkbox" name="ProductCode2"
value="LL64"> 
</td>
</tr></table> 
</td> 
<td valign="top" width="25%"> 
<table width="100%"cellpadding="0" cellspacing="0"><tr>
<td valign="top">
<b><FONT class="pricecolorsmall colors_productprice">Sale Price: £8.50 </font></b>
<br /><div class="337">Add</div> <input type="checkbox" name="ProductCode2"
value="54330"> 
</td>
</tr></table> 
</td> 

<td valign="top" width="25%"> 
<table width="100%"cellpadding="0" cellspacing="0"><tr>
<td valign="top">
<b><FONT class="pricecolorsmall colors_productprice">Sale Price: £4.95 </font></b>
<br /><div class="337">Add</div> <input type="checkbox" name="ProductCode2"
value="460GCRS"> 
</td>
</tr></table>

3 个答案:

答案 0 :(得分:3)

您可以将要删除的值放在数组中,并过滤这些值。

var valuesToRemove = ["460GCRS","54330","480GCFD"];

$('input').filter(function() {
    return $.inArray(this.value, valuesToRemove) > -1;
}).prev().remove().end().remove();​

以下示例删除了您发布的4个输入中的3个:http://jsfiddle.net/3aPLU/

答案 1 :(得分:0)

此代码段会删除名称为<input>的所有ProductCode2个元素。如果这不是你需要的,请告诉我。

$(function() {
    $('input[name="ProductCode2"]').each(function() {
        $(this).prev('div').remove();
        $(this).remove();
    });
});

答案 2 :(得分:0)

要按值查找复选框,您可以使用jQuery,然后调用remove

 $('input[value="ValueYouAreLookingFor"]').remove();

要在上述调用之前删除div,您可以执行相同的操作,但后退并删除div

$('input[value="ValueYouAreLookingFor"]').prev('div').remove();
相关问题