PHP-复选框组
我有一个复选框组在一个表格,我需要张贴PHP-复选框组,php,forms,Php,Forms,我有一个复选框组在一个表格,我需要张贴 <input type="checkbox" value="true" checked name="chk0[]"> <input type="checkbox" value="false" name="chk0[]"> <input type="checkbox" value="false" name="chk0[]"> <input type="checkbox" value="true" checked na
<input type="checkbox" value="true" checked name="chk0[]">
<input type="checkbox" value="false" name="chk0[]">
<input type="checkbox" value="false" name="chk0[]">
<input type="checkbox" value="true" checked name="chk0[]">
<input type="checkbox" value="true" checked name="chk1[]">
<input type="checkbox" value="false" name="chk1[]">
<input type="checkbox" value="true" checked name="chk1[]">
<input type="checkbox" value="false" name="chk1[]">
我如何确保获得如下所示的发布值:
[chk0] => Array (
[0] => true
[3] => true
)
[chk1] => Array (
[0] => true
[2] => true
)
我想知道复选框的键,而不是按顺序显示
谢谢明确提供您的钥匙
<input type="checkbox" value="true" checked name="chk0[0]">
<input type="checkbox" value="false" name="chk0[1]">
<input type="checkbox" value="false" name="chk0[2]">
<input type="checkbox" value="true" checked name="chk0[3]">
<input type="checkbox" value="true" checked name="chk1[0]">
<input type="checkbox" value="false" name="chk1[1]">
<input type="checkbox" value="true" checked name="chk1[2]">
<input type="checkbox" value="false" name="chk1[3]">
明白我的意思吗?
框的工作原理与PHP中的数组元素类似。您可以指定索引,它们将作为$\u POST['chk0'][1]
元素按原样传输到PHP中。使用复选框名称中的键,如name=“chk[3]”“
而不是name=“chk[]”
我同意答案1,但问题是,我正在使用父复选框切换子类别复选框。如果使用chk0[0],chk0[1]等,则ToggleParentCheckBox函数将失败。。。请看一下:
<html>
<head>
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");
</script>
<script language="JavaScript">
function toggleTableRows()
{
$(document).ready(function() {
$('img.parent')
.css("cursor","pointer")
.toggle(
function() {
$(this).attr("title","Click to Collapse")
$(this).attr("src","arrow_expanded.gif");
$('tr').siblings('#child-'+this.id).toggle();
},
function() {
$(this).attr("title","Click to Expand");
$(this).attr("src","arrow_collapsed.gif");
$('tr').siblings('#child-'+this.id).toggle();
}
);
initCheckBoxes();
});
}
function toggleCheckboxes(current, form, field) {
$("#"+ form +" :checkbox[name='"+ field +"[]']").attr("checked", current.checked);
}
function toggleParentCheckboxes(current, form) {
var checked = ($("#"+ form +" :checkbox[name='"+ current.name +"']").length == $("#"+ form +" :checkbox[name='"+ current.name +"']:checked").length);
$("#"+ form +" :checkbox[name='"+ current.name.replace("[]", "") +"']").attr("checked", checked);
}
function initCheckBoxes() {
$("#frmDinnerMenu :checkbox:checked").each(function() {
if (this.name.replace(/[0-9]/g, "") == "chk[]") {
toggleParentCheckboxes(this, "frmDinnerMenu");
}
});
}
</script>
<script language="JavaScript">toggleTableRows();</script>
</head>
<body>
<form name="frmDinnerMenu" id="frmDinnerMenu" method="POST" action="">
<table border=1>
<tr>
<td><img class="parent" id="0" src="arrow_collapsed.gif" title="Click to Expand">Category - Fruits</td>
<td><input type="checkbox" name="chk0" onclick="toggleCheckboxes(this, 'frmDinnerMenu', 'chk0');"/></td>
</tr>
<tr style="display: none;" id="child-0">
<td> Apple</td>
<td><input type="checkbox" value="0" name="chk0[0]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
</tr>
<tr style="display: none;" id="child-0">
<td> Banana</td>
<td><input type="checkbox" checked value="0" name="chk0[1]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
</tr>
<tr style="display: none;" id="child-0">
<td> Orange</td>
<td><input type="checkbox" checked value="0" name="chk0[2]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
</tr>
<tr><td><img class="parent" id="1" src="arrow_collapsed.gif" title="Click to Expand">Category - Vegetables</td><td><input type="checkbox" name="chk1" onclick="toggleCheckboxes(this, 'frmDinnerMenu', 'chk1');"/></td></tr>
<tr style="display: none;" id=child-1><td> Cabbage</td><td><input type="checkbox" checked value="0" name="chk1[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-1><td> Tomatoes</td><td><input type="checkbox" checked value="0" name="chk1[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-1><td> Green Peppers</td><td><input type="checkbox" checked value="0" name="chk1[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr><td><img class="parent" id="2" src="arrow_collapsed.gif" title="Click to Expand">Category - Dessert</td><td><input type="checkbox" name="chk2" onclick="toggleCheckboxes(this, 'frmDinnerMenu', 'chk2');"/></td></tr>
<tr style="display: none;" id=child-2><td> Ice Cream</td><td><input type="checkbox" checked value="0" name="chk2[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-2><td> Custard</td><td><input type="checkbox" checked value="0" name="chk2[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-2><td> Chocolate Cake</td><td><input type="checkbox" checked value="0" name="chk2[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
</table>
</form>
</body>
</html>
load(“jquery”,“1.3.2”);
google.load(“jqueryui”,“1.7.2”);
函数toggleTableRows()
{
$(文档).ready(函数(){
$('img.parent')
.css(“光标”、“指针”)
.切换(
函数(){
$(this.attr(“标题”,“单击以折叠”)
$(this.attr(“src”,“arrow_expanded.gif”);
$('tr').sides('#child-'+this.id).toggle();
},
函数(){
$(this.attr(“标题”,“单击展开”);
$(this.attr(“src”,“arrow_collapsed.gif”);
$('tr').sides('#child-'+this.id).toggle();
}
);
初始化复选框();
});
}
功能切换复选框(当前、窗体、字段){
$(“#”+form+”:复选框[name=”“+field+“[]”]).attr(“选中”,当前.checked);
}
函数切换父复选框(当前、窗体){
var checked=($(“#“+form+”:复选框[name=”“+current.name+”))。长度==$(“#“+form+”:复选框[name=”+current.name+“]:checked”)。长度);
$(“#”+form+”:复选框[name=”+current.name.replace(“[]”,“)+“]”).attr(“选中”,选中);
}
函数初始化复选框(){
$(“#frmdinermenu:checkbox:checked”)。每个(函数(){
if(this.name.replace(/[0-9]/g,”)==“chk[]”){
切换父项复选框(此为“frmdinermenu”);
}
});
}
切换表行();
类别-水果
苹果
香蕉
橙色
类别-蔬菜
卷心菜
西红柿
青椒
类别-甜点
冰淇淋
蛋奶沙司
巧克力蛋糕
我怎样才能解决这个问题,所以切换工作正常,post变量也是我想要的
谢谢为什么投反对票?我错过什么了吗?。。。(讨厌人们否决投票,然后一字不提就跑掉。这是一个完全不同于您最初提出的问题。您应该创建一个新的问题提交。为什么要索引?将id作为每个复选框的值不是更好吗?
<input type="checkbox" value="true" name="chk[0][0]" checked>
<input type="checkbox" value="false" name="chk[0][1]">
<input type="checkbox" value="false" name="chk[0][2]">
<input type="checkbox" value="true" name="chk[0][3]" checked>
<input type="checkbox" value="true" name="chk[1][0]" checked>
<input type="checkbox" value="false" name="chk[1][1]">
<input type="checkbox" value="true" name="chk[1][2]" checked>
<input type="checkbox" value="false" name="chk[1][3]">
Array
(
[chk] => Array
(
[0] => Array
(
[0] => true
[3] => true
)
[1] => Array
(
[0] => true
[2] => true
)
)
)
<input type="checkbox" value="true" checked name="chk0[0]">
<input type="checkbox" value="false" name="chk0[1]">
<input type="checkbox" value="false" name="chk0[2]">
<input type="checkbox" value="true" checked name="chk0[3]">
<input type="checkbox" value="true" checked name="chk1[0]">
<input type="checkbox" value="false" name="chk1[1]">
<input type="checkbox" value="true" checked name="chk1[2]">
<input type="checkbox" value="false" name="chk1[3]">
<html>
<head>
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");
</script>
<script language="JavaScript">
function toggleTableRows()
{
$(document).ready(function() {
$('img.parent')
.css("cursor","pointer")
.toggle(
function() {
$(this).attr("title","Click to Collapse")
$(this).attr("src","arrow_expanded.gif");
$('tr').siblings('#child-'+this.id).toggle();
},
function() {
$(this).attr("title","Click to Expand");
$(this).attr("src","arrow_collapsed.gif");
$('tr').siblings('#child-'+this.id).toggle();
}
);
initCheckBoxes();
});
}
function toggleCheckboxes(current, form, field) {
$("#"+ form +" :checkbox[name='"+ field +"[]']").attr("checked", current.checked);
}
function toggleParentCheckboxes(current, form) {
var checked = ($("#"+ form +" :checkbox[name='"+ current.name +"']").length == $("#"+ form +" :checkbox[name='"+ current.name +"']:checked").length);
$("#"+ form +" :checkbox[name='"+ current.name.replace("[]", "") +"']").attr("checked", checked);
}
function initCheckBoxes() {
$("#frmDinnerMenu :checkbox:checked").each(function() {
if (this.name.replace(/[0-9]/g, "") == "chk[]") {
toggleParentCheckboxes(this, "frmDinnerMenu");
}
});
}
</script>
<script language="JavaScript">toggleTableRows();</script>
</head>
<body>
<form name="frmDinnerMenu" id="frmDinnerMenu" method="POST" action="">
<table border=1>
<tr>
<td><img class="parent" id="0" src="arrow_collapsed.gif" title="Click to Expand">Category - Fruits</td>
<td><input type="checkbox" name="chk0" onclick="toggleCheckboxes(this, 'frmDinnerMenu', 'chk0');"/></td>
</tr>
<tr style="display: none;" id="child-0">
<td> Apple</td>
<td><input type="checkbox" value="0" name="chk0[0]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
</tr>
<tr style="display: none;" id="child-0">
<td> Banana</td>
<td><input type="checkbox" checked value="0" name="chk0[1]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
</tr>
<tr style="display: none;" id="child-0">
<td> Orange</td>
<td><input type="checkbox" checked value="0" name="chk0[2]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
</tr>
<tr><td><img class="parent" id="1" src="arrow_collapsed.gif" title="Click to Expand">Category - Vegetables</td><td><input type="checkbox" name="chk1" onclick="toggleCheckboxes(this, 'frmDinnerMenu', 'chk1');"/></td></tr>
<tr style="display: none;" id=child-1><td> Cabbage</td><td><input type="checkbox" checked value="0" name="chk1[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-1><td> Tomatoes</td><td><input type="checkbox" checked value="0" name="chk1[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-1><td> Green Peppers</td><td><input type="checkbox" checked value="0" name="chk1[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr><td><img class="parent" id="2" src="arrow_collapsed.gif" title="Click to Expand">Category - Dessert</td><td><input type="checkbox" name="chk2" onclick="toggleCheckboxes(this, 'frmDinnerMenu', 'chk2');"/></td></tr>
<tr style="display: none;" id=child-2><td> Ice Cream</td><td><input type="checkbox" checked value="0" name="chk2[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-2><td> Custard</td><td><input type="checkbox" checked value="0" name="chk2[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-2><td> Chocolate Cake</td><td><input type="checkbox" checked value="0" name="chk2[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
</table>
</form>
</body>
</html>