如果一個(gè)表單中有多個(gè)同名復(fù)選框,在提交到php時(shí)卻只有一個(gè)值,而并不像asp那樣是一串用逗號(hào)分割的值。有一個(gè)很簡(jiǎn)單的方法來解決:將復(fù)選框的name后面加上[],例如:<input type="checkbox" name="ccc" value="1"> 改為:<input type="checkbox" name="ccc[]" value="1">。這樣php將得到一個(gè)叫ccc的陣列。但這種方法有個(gè)問題,如果您要在客戶端對(duì)復(fù)選框是否被選擇、選擇了幾個(gè)用javascript來判斷時(shí),javascript會(huì)因?yàn)閺?fù)選框的name中含有[]而出錯(cuò)。您可以在表單中加入一個(gè)隱含域,用javascript設(shè)置它的值。
<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" onsubmit="check()"> <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>
|
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!