需要从html数组中删除值

时间:2018-01-24 16:59:15

标签: javascript html arrays

我使用数组来获取html中每行的输入值。 代码是

<input type="text" name="source[]" id="source'+row'">

当我删除一行时,我需要从数组中删除元素。

var source=document.getElementsByName("source");
 Array.prototype.forEach.call( source, function( node,index ) {
    if(index==id){
        node.parentNode.removeChild( node );
    }
});

当我控制结果时,孩子被移除。但是当我使用PHP提交表单时,数组的值为空。

`['24','','',56]`

如何删除空值。

2 个答案:

答案 0 :(得分:1)

它正常工作,再次检查你在做什么。

.pos-menu:hover .hvr-sweep-to-bottom { 
    background: rgba(0,0,0,.7); 
}

(这是一个期望为<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script> function test(){ var source=document.getElementsByName("source[]"); Array.prototype.forEach.call( source, function( node,index ) { if(index===1){ node.parentNode.removeChild( node ); } }); } </script> </head> <body> <?php var_dump($_GET); ?> <hr> <form action="index.php"> <input type="text" name="source[]"><br> <input type="text" name="source[]"><br> <input type="text" name="source[]"><br> <input type="text" name="source[]"><br> <input type="submit" value="sub"> </form> <button onclick="test()">test</button> </body> </html> 的PHP文件,将数组发送给自己,按钮index.php删除索引== 1的元素

答案 1 :(得分:0)

从数组中删除空值的简单方法是:

var this_array = this_array.filter(function(x){
  return (x !== (undefined || null || ''));
});