2

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

 <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了?

4

3 回答 3

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();
}
于 2013-10-14T08:49:09.380 回答
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);
于 2013-10-14T09:11:34.200 回答
0
$("DIV#21, INPUT[value='21']").remove()
于 2013-10-14T08:49:10.787 回答