Html里面多个checkbox 回显
时间: 2023-10-06 14:13:46 浏览: 139
要回显多个 checkbox 的选中状态,可以使用 HTML 和 PHP 实现。以下是具体步骤:
1. 在 HTML 表单中使用多个 checkbox,每个 checkbox 都应该有一个唯一的 name 属性值,例如:
```html
<input type="checkbox" name="fruits[]" value="apple"> 苹果
<input type="checkbox" name="fruits[]" value="banana"> 香蕉
<input type="checkbox" name="fruits[]" value="orange"> 橘子
<input type="checkbox" name="fruits[]" value="watermelon"> 西瓜
```
2. 在 PHP 代码中获取 checkbox 的值并将其存储到数组中,例如:
```php
$fruits = isset($_POST['fruits']) ? $_POST['fruits'] : array();
```
这里使用了 isset() 函数检查是否选中了水果,如果选中了,则将选中的水果存储到 $fruits 数组中。
3. 在 HTML 中使用 PHP 代码回显 checkbox 的选中状态,例如:
```html
<input type="checkbox" name="fruits[]" value="apple" <?php if(in_array('apple', $fruits)) echo 'checked'; ?>> 苹果
<input type="checkbox" name="fruits[]" value="banana" <?php if(in_array('banana', $fruits)) echo 'checked'; ?>> 香蕉
<input type="checkbox" name="fruits[]" value="orange" <?php if(in_array('orange', $fruits)) echo 'checked'; ?>> 橘子
<input type="checkbox" name="fruits[]" value="watermelon" <?php if(in_array('watermelon', $fruits)) echo 'checked'; ?>> 西瓜
```
在上面的代码中,使用 in_array() 函数检查选项是否已经被选中,并在选项中添加 checked 属性以回显选中状态。
以上就是回显多个 checkbox 的选中状态的具体步骤。
阅读全文
相关推荐


















