【问题标题】:cannot make radio buttons sticky不能使单选按钮变粘
【发布时间】:2014-11-15 01:04:38
【问题描述】:
所以我设法让我的单选按钮变得粘滞,但是这段代码发生的事情是用户现在可以选择多个选项(他们应该只能选择一个),而且,一旦你选择了一个选项你不能取消选择它。
这是我现在的代码:
How did you hear about me?<br/>
<input type="radio" name="response_fb" value="true"
<?php if (isset($_POST['response_fb']) == 'true') echo ' checked'; ?>>Facebook/online<br>
<input type="radio" name="response_wordofmouth" value="true"
<?php if (isset($_POST['response_wordofmouth']) == 'true') echo ' checked'; ?>>Word of mouth<br>
<input type="radio" name="response_other" value="true"
<?php if (isset($_POST['response_other']) == 'true') echo ' checked'; ?>>Other
有人可以帮忙吗?
提前致谢!
【问题讨论】:
标签:
php
html
forms
radio-button
sticky
【解决方案1】:
<input type="radio" name="response" value="facebook"
<?php if (isset($_POST['response']) and $_POST['response'] == 'facebook') echo ' checked'; ?>>Facebook/online<br>
<input type="radio" name="response" value="wordofmouth"
<?php if (isset($_POST['response']) and $_POST['response'] == 'wordofmouth') echo ' checked'; ?>>Word of mouth<br>
<input type="radio" name="response" value="other"
<?php if (isset($_POST['response']) and $_POST['response'] == 'other') echo ' checked'; ?>>Other
你必须设置相同的名称和不同的值!
【解决方案2】:
所有单选按钮的名称 attr 必须相同。这就是 HTML 如何确定只能选择组中的一个。
编辑:我认为这更像是你的目标
<?php if (isset($_POST['response']) == 'true') echo '$_POST['response'] was checked'; ?>
<input type="radio" name="response" value="facebook">
<input type="radio" name="response" value="word_of_mouth">
<input type="radio" name="response" value="other">