value属性应该包含在选中复选框时将发送到服务器的值。如果未选中复选框,则不会发送任何内容。

<label for="checkboxField">A checkbox field</label> 
<input type="checkbox" name="checkboxField" id="checkboxField" value="yes" /> 

您可以通过向输入标记添加checked =“checked"属性来预先选择复选框:

<input type="checkbox" checked="checked" ... />. 

通过创建具有相同name属性的多个复选框字段,您可以允许用户选择同一字段的多个值。

以下脚本用于index.htm。 它有几个复选框。

<form action ="index.php"> <li><input type ="checkbox" name ="chkFries" value ="11.00">Fries</li> <li><input type ="checkbox" name ="chkSoda" value ="12.85">Soda</li> <li><input type ="checkbox" name ="chkShake" value ="1.30">Shake</li> <li><input type ="checkbox" name ="chkKetchup" value =".05">Ketchup</li> <input type ="submit"> </form> </body> </html>

以下代码用于index.php,它接受来自复选框的值。

print "chkFries:" . $chkFries . "<br/>"; print "chkSoda:" $chkSoda . "<br/>"; print "chkShake:" . $chkShake . "<br/>"; print "chkKetchup" . $chkKetchup . "<br/>"; $total = 0; if (!empty($chkFries)){ print ("You chose Fries <br>"); $total = $total + $chkFries; if (!empty($chkSoda)){ print ("You chose Soda <br>"); $total = $total + $chkSoda; if (!empty($chkShake)){ print ("You chose Shake <br>"); $total = $total + $chkShake; if (!empty($chkKetchup)){ print ("You chose Ketchup <br>"); $total = $total + $chkKetchup; print "The total cost is \$$total";