2013-01-21 23 views
0

我有一个脚本,我使用复选框和JavaScript来显示其他项目,如果复选框被选中。 这似乎在大多数情况下工作得很好。 但有一个复选框会出现问题。 我认为是因为与它相关的javascript魔术。 检查并取消选中时,该复选框始终在发布后返回isset。复选框与JavaScript魔术附加不会取消选中

从不选中复选框并提交回报,因为它没有设置。 检查并提交返回检查,因为它应该 检查,取消选中并提交返回...检查! 我已经成立了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

请在这里发布相关的代码。无论如何,这个问题不会对任何人有用,除了你,将会因为本地化而关闭。 –

回答

2

该复选框命名为se_currentitemselect菜单命名为se_currentitem[]。 PHP的$_POST数组将这些视为相同。

  • 当您选中该框时,您将创建select菜单。
  • 当您取消选中该框时,您将隐藏select菜单,但它仍保留在DOM中,并且由浏览器提交。请注意网络检查员(或tcpflow等)。浏览器同时提交se_currentitemse_currentitem[]

您应该重命名复选框,以便它不叫se_currentitem(或重命名select菜单,以便它不叫se_currentitem[])。

+0

非常感谢你!我一直在这方面挣扎数小时! 我不知道这会是一个问题。 – bolvo