按其值删除隐藏字段

时间:2013-10-14 08:46:17

标签: javascript mootools

我有一个页面,其中包含一些如下所示的值:

 <div id="productList" >
   <input type="hidden" name="product[0].id" value="21">
   <div id="21" >BeanCounter</div>
   <input type="hidden" name="product[1].id" value="22">
   <div id="22" >CallGate</div>
</div>

现在我要删除隐藏字段value=21和div id=21

我有这个:

function remove(id){
    var remove = $(id);
    $('productList').removeChild(remove);

但是如何删除隐藏字段?你能用它的价值得到一个对象吗?

编辑错过了mootools标签,不确定jquery标签的来源。我猜mootools并不常见,但希望这可以用普通的javascript来完成。

嗯标签又变回了jquery?

3 个答案:

答案 0 :(得分:3)

Mootools解决方案:

function remove(id){
    $('productList').getElements('input[value="' + id + '"], div[id="' + id + '"]').destroy();
}

jQuery解决方案:

function remove(id){
    $('input[value="' + id + '"], div[id="' + id + '"]').remove();
}

答案 1 :(得分:2)

在编辑后的帖子中使用plain javascript

var checkVal = '21';

var parentDiv = document.getElementById('productList');
var inps = parentDiv.getElementsByTagName('input');
var inpRemove = null;

//Get the div to be removed
var divRemove = document.getElementById(checkVal);

for(var i=0; i<inps.length; i++)
{
   if(inps[i].value == checkVal)
   {
      //Get the input to be removed (Assuming only input with same value)
      inpRemove = inps[i];
      break;
   }
}

//Remove both here
parentDiv.removeChild(inpRemove);
parentDiv.removeChild(divRemove);

答案 2 :(得分:0)

$("DIV#21, INPUT[value='21']").remove()
相关问题