2013-01-21 26 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