我有一个脚本,如果选中复选框,我会使用复选框和javascript来显示其他项目。 这似乎在大多数情况下工作得很好。 然而,有一个复选框会产生问题。 我假设因为与它相关的javascript魔法。 检查它然后取消选中它时,复选框始终在post后返回isset。
永远不要选中复选框并提交未按预期设置的退货。 检查和提交退货应检查 检查,取消选中和提交退货...已检查! 我在http://vampke.uphero.com/tst.php
上设置了一个示例以下是代码:
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(isset($_POST['se_currentitem'])) echo "CHECKBOX = ISSET";
else echo "CHECKBOX = NOT SET";
}
echo <<< EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
.hiddenDiv {display: none;}
.visibleDiv{display: block;}
</style>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
var currentitem = 0;
function toggle_currentitem(){
mydiv = document.getElementById("currentitemcontainer");
if (document.getElementById('se_currentitem').checked){
mydiv.className = "visibleDiv";
if(currentitem==0){
addcurrentitem();
}
}
else {
mydiv.className = "hiddenDiv";
}
}
function addcurrentitem(){
currentitem++;
var newitem = document.createElement('div');
newitem.id = currentitem;
newitem.innerHTML= "<p><strong><em>new item</em></strong><br /><label>select a number:</label><select name='se_currentitem[]'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select></p>";
document.getElementById('currentitem').appendChild(newitem);
}
//-->
</script>
<form action ="" method="post">
<input type="checkbox" id="se_currentitem" name="se_currentitem" value="1" onchange="toggle_currentitem()" /><label for="se_currentitem">click the checkbox to activate the items</label> <br />
<div id="currentitemcontainer" class="hiddenDiv">
<div id="currentitem"></div>
<a id='addnewcurrent' onclick='addcurrentitem()'>add item</a>
</div>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
有谁知道发生了什么事?
答案 0 :(得分:2)
该复选框名为se_currentitem
,select
菜单名为se_currentitem[]
。 PHP的$_POST
数组将它们视为相同。
select
菜单。select
菜单,但它仍保留在DOM中,并由浏览器提交。请注意网络检查器(或tcpflow等)。浏览器同时提交se_currentitem
和se_currentitem[]
。您应该重命名该复选框,使其不会被调用se_currentitem
(或重命名select
菜单,使其不会被称为se_currentitem[]
。)