Javascript 需要从html数组中删除值
我使用数组来获取html中每一行的输入值。 代码是Javascript 需要从html数组中删除值,javascript,html,arrays,Javascript,Html,Arrays,我使用数组来获取html中每一行的输入值。 代码是 <input type="text" name="source[]" id="source'+row'"> 当我控制结果时,子对象被移除。但是当我使用PHP提交表单时,数组中有空值 `['24','','',56]` 如何删除空值。从数组中删除空值的简单方法是: `['24','','',56]` var this_array = this_array.filter(function(x){ return (x !== (
<input type="text" name="source[]" id="source'+row'">
当我控制结果时,子对象被移除。但是当我使用PHP提交表单时,数组中有空值
`['24','','',56]`
如何删除空值。从数组中删除空值的简单方法是:
`['24','','',56]`
var this_array = this_array.filter(function(x){
return (x !== (undefined || null || ''));
});
它只是工作,再次检查你在做什么
<!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>
功能测试(){
var source=document.getElementsByName(“source[]);
Array.prototype.forEach.call(源、函数(节点、索引){
如果(索引==1){
node.parentNode.removeChild(节点);
}
});
}
测试
(这是一个PHP文件,希望是
index.PHP
,将数组发送到自身,按钮test
删除索引=1的元素)我不完全理解您的问题,但快速解决方法是在发送数组之前根据其元素值过滤数组。例如:source.filter(item=>item);这将删除空值。var是源和数组吗?身份证从哪里来?唯一的错误是源数组不正确吗?如果您发布了更多的代码,这样我们就可以获得context,这会有所帮助。如果输入是该行的子节点,并且您使用removeChild或类似工具删除该行,它将自动删除输入。感谢您的想法,我在php文件中修复了这些问题