我創建了以下的文件夾table
,它爲用戶提供了使用複選框來選擇選項的選項。我將選擇的數量限制爲四個,但它選擇了四個以上的選擇。限制用戶在PHP中選擇的複選框的數量
HTML:
<table width="100%" border="0">
<tr>
<th width="37%" height="19" align="center" bgcolor="#CCCCCC">
<strong>Biological</strong>
</th>
<th width="37%" align="center" bgcolor="#CCCCCC">
<strong>Psychological</strong>
</th>
<th width="37%" align="center" bgcolor="#CCCCCC">
<strong>Social</strong>
</th>
</tr>
<tr>
<td>
<input type="radio" name="Antidepressant" id="Antidepressant" value="Antidepressant" onclick="chkcontrol(1)" />
<label for="Antidepressant"></label>Antidepressant</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<input type="radio" name="Antipsychotic oral" id="Antipsychotic oral" value="Antipsychotic oral" onclick="chkcontrol(2)" />
<label for="Antipsychotic oral"></label>Antipsychotic oral</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<input type="radio" name="Antipsychotic_depot" id="Antipsychotic depot" value="Antipsychotic depot" onclick="chkcontrol(3)" />
<label for="Antipsychotic depot"></label>Antipsychotic depot</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<input type="radio" name="Bblocker" id="B-blocker" value="B-blocker" onclick="chkcontrol(4)" />
<label for="B-blocker"></label>B-blocker</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<input type="radio" name="Benzodiazepine" id="Benzodiazepine" value="Benzodiazepine" onclick="chkcontrol(5)" />
<label for="Benzodiazepine"></label>Benzodiazepine</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<input type="radio" name="Mood_stabiliser" id="Mood stabiliser" value="Mood stabiliser" onclick="chkcontrol(6)" />
<label for="Mood stabiliser"></label>Mood stabiliser</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<input type="radio" name="ECT" id="ECT" value="ECT" onclick="chkcontrol(7)" />
<label for="ECT"></label>ECT</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<input type="radio" name="Otherbio" id="Other" value="Other" />
<label for="Other"></label>Other</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
用於CHEC選擇數量的腳本:
function chkcontrol(j) {
var total = 0;
for (var i = 0; i < document.form1.ckb.length; i++) {
if (document.form1.ckb[i].checked) {
total = total + 1;
}
if (total > 4) {
alert("Please Select only three")
document.form1.ckb[j].checked = false;
return false;
}
}
}
以下是截圖:
但腳本無法正常工作並且允許用戶選擇4個以上的選項 – Tina
PHP與此有什麼關係?其唯一的javascript .. – SuperSaiyan
首發,那些單選按鈕不復選框。 – tradyblix