【发布时间】:2017-12-31 11:32:59
【问题描述】:
我有一个 html 表格,每行都有复选框。在选中每个复选框时,我将行值推送到名为 checkboxArrayForOperations 的数组中。
HTML
<table>
<?php
$i = 0;
foreach ($databaseValue as $stValue) {
$i++;
?>
<tr>
<input type="text" id="stName<?php echo $i;?>" value="<?php echo $stValue['stName'];?>">
<input type="text" id="stId<?php echo $i;?>" value="<?php echo $stValue[0]['stId'];?>">
<td><input type="checkbox" class="checkboxForOperations" value="<?php echo $i;?>" onclick="checkBoxForOperations(this)"></td>
</tr>
<?php
}
?>
</table>
JS
var checkboxArrayForOperations = []; // global array
function checkBoxForOperations(obj){
var checkboxValue = obj.value;
if(obj.checked) {
checkboxArray = [];
checkboxArray["stName"] = $('#stName'+checkboxValue).val();
checkboxArray["stId"] = $('#stId'+checkboxValue).val();
checkboxArrayForOperations.push(checkboxArray);
} else {
var itemArrayForRemoval = [];
itemArrayForRemoval["stName"] = $('#stName'+checkboxValue).val();
itemArrayForRemoval["stId"] = $('#stId'+checkboxValue).val();
var indexItemArrayForRemoval = index(itemArrayForRemoval);
checkboxArrayForOperations = checkboxArrayForOperations.filter( function( el ) {
return !(JSON.stringify(el) in itemArrayForRemoval);
} );
}
console.log(checkboxArrayForOperations);
}
function index(arr) {
return arr.reduce(function(acc, item){
return acc[JSON.stringify(item)] = item, acc
}, {})
}
取消选中我想删除与该未选中行对应的元素(作为数组添加)。我指的是answer。但该数组并未从checkboxArrayForOperations 中删除。该数组看起来如JSFiddle中所示
我做错了什么?谁能帮我解决这个问题。提前致谢。
【问题讨论】:
-
您是不是在单击复选框时将复选框的值添加到数组中,然后在取消选中时将其删除?
-
@GrahamS。我将与选中行对应的 stname 和 stid 值保存到数组中。
-
很酷,您最终会尝试将所有检查的数据发送到服务器,对吧?
-
@GrahamS..是的。在取消选中时,如果已添加(已选中),我想删除与该行对应的数据。
标签: javascript php jquery arrays checkbox