如果一個表單中有多個同名復選框,在提交到php時卻只有一個值,而并不像asp那樣是一串用逗號分割的值。有一個很簡單的方法來解決:將復選框的name后面加上[],例如:<input type="checkbox" name="ccc" value="1"> 改為:<input type="checkbox" name="ccc[]" value="1">。這樣php將得到一個叫ccc的陣列。但這種方法有個問題,如果您要在客戶端對復選框是否被選擇、選擇了幾個用javascript來判斷時,javascript會因為復選框的name中含有[]而出錯。您可以在表單中加入一個隱含域,用javascript設置它的值。
| <script language="javascript"> function check() { var strchoice=""; for(var i=0;i<document.news.choice.length;i ) { if (document.news.choice[i].checked) { strchoice=strchoice document.news.choice[i].value ","; } } if (!document.news.choice.length) { if (document.news.choice.checked) { strchoice=document.news.choice[i].value; "," } } strchoice=strchoice.substring(0,strchoice.length-1); document.news.choiceid.value=strchoice; alert(document.news.choiceall.value); } </script> <html> ... <form name="news" action="test.php" method="post" > <input type="checkbox" name="choice" value="1"> <input type="checkbox" name="choice" value="2"> <input type="checkbox" name="choice" value="3"> <input type="checkbox" name="choice" value="4"> <input type="hidden" name="choiceid" value=""> </form> ... </html> |
新聞熱點
疑難解答