2012-10-15 87 views
0

我試圖在javascript的幫助下動態添加複選框。但是我的java腳本只替換了早先的元素。我需要將新選定的元素添加到現有列表中。這是代碼。請幫忙。需要動態添加複選框

這是index.php文件

<html> 
<head> 
<script> 
function select_item(item,price) 
{ 
if (item.length==0 || price.length==0) 
    { 
    document.getElementById("order").innerHTML=""; 
    return; 
    } 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("order").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","order.php?item="+item+"&price="+price,true); 
xmlhttp.send(); 
} 
</script> 
</head> 
<div id="order"></div> 
</html> 
<?php 
echo "<a href=\"#\" onClick = \"select_item('Value1','Price1'); return false;\">Some Item1 here</a>"; 
echo "<br>"; 
echo "<a href=\"#\" onClick = \"select_item('Value2','Price2'); return false;\">Some Item2 here</a>"; 
?> 

這是order.php

<?php 
$item=$_GET["item"]; 
echo "<form action=\"#\">"; 
echo "<input type=\"checkbox\" checked=\"yes\" name=\"vehicle\" value=\"Bike\">$item<br>"; 
echo "</form>"; 
?> 

回答

1

您可以使用此:

<form action .... 
<div id="order"></div> 
</form> 

document.getElementById("order").innerHTML+=xmlhttp.responseText; 

和order.php

<?php 
$item=$_GET["item"]; 
echo "<input type=\"checkbox\" checked=\"yes\" name=\"vehicle\" value=\"Bike\">$item<br>"; 
?> 
+0

謝謝老兄!工作! :d – user1051505