我要求未选中复选框的值为' 0'所以我可以确定已经检查了哪些值以及要发布的所有值(选中+未选中)($ _POST [' chk])。我正在使用for循环来创建复选框,我正在生成一个隐藏字段,其值为' 0' (正如在stackoverflow上建议elsewhere)。
有4个复选框((count($ result)-1)= 4)和复选框1&复选框2& 4未经检查,$ _POST [' chk']数组最终结果如下:
Array
(
[chk] => Array
(
[0] => 0
[1] => 180.00
[2] => 0
[3] => 0
[4] => 100.00
[5] => 0
)
我希望它看起来像这样:
Array
(
[chk] => Array
(
[0] => 180.00
[1] => 0
[2] => 100.00
[3] => 0
)
我做错了什么?甚至可以使用for循环和隐藏的复选框字段吗?
使用Javascript:
function calculate() {
var el, i = 0;
var subtotal = 0;
while(el = document.getElementsByName("chk[]")[i++]) {
if(el.checked) { subtotal = subtotal + Number(el.value);}
}
var node = document.getElementById("subtotal");
node.innerHTML = "$" + subtotal + ".00";
var node = document.getElementById("total");
node.innerHTML = "$" + (subtotal*=$no_nights?>) + ".00";
}
HTML / PHP:
for ($x=0; $x<=(count($result)-1); $x++) { ?>
} ?>