首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保持单选按钮值并设置默认选中

保持单选按钮值并设置默认选中
EN

Stack Overflow用户
提问于 2014-04-22 22:11:49
回答 2查看 3.1K关注 0票数 1

我有一个单选按钮组的表格。我想将“选中”设置为默认的第二个单选按钮,如果用户单击第一个单选按钮,则在提交表单后保留该值。

请注意,我使用<span class="custom-radiobutton"></span>作为无线电按钮的样式,它的z-index比真正的有opacity:0<input type="radio"要低。

这是我的代码片段:

代码语言:javascript
复制
<div class="group-wrapper">
  <div class="radiobutton-wrapper boolean">
    <span class="custom-radiobutton"></span>
    <input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if ((isset($_POST['hosting'])) && ((isset($_POST['hosting'])) == 1)) {echo 'checked="checked"';}; ?> /> 
    <label for="hosting-1">Sí</span>
  </div>
  <div class="radiobutton-wrapper boolean">
    <span class="custom-radiobutton"></span>
    <input type="radio" id="hosting-2" name="hosting[]" value="0" class="w100" <?php if ((!isset($_POST['hosting'])) || ((isset($_POST['hosting'])) == 0)) {echo 'checked="checked"';}; ?> /> 
    <label for="hosting-2">No</label>
  </div>
</div>

附加信息:

  • 我在用HTML5。
  • 我正在用PHP验证表单(我想保留这个,即使我知道可能更好的jQuery+PHP验证)。
  • 我注意到我需要两次点击来选择第一个单选按钮。这只发生在原始状态。在此之后,按照预期的方式,只需单击一次即可工作。

我花了很多时间试图找出出了什么问题,所以任何帮助都是非常感谢的。干杯,

EN

回答 2

Stack Overflow用户

发布于 2015-12-17 11:35:59

代码语言:javascript
复制
<input type="radio" id="hosting-1" name="hosting[]" class="w100"  checked="checked" /> 1

在你的代码中试试这个。

例如:

代码语言:javascript
复制
<tr>
    <td><br><b>Reservation Handling :  <span style="color:#FF0000">* &nbsp;&nbsp;</span></td>
    <td><br><input type="radio" name="reservation" value="Delighted" required> Delighted<br></input></td>
</tr>
<tr>
    <td> </td>
    <td><input type="radio" name="reservation" checked="checked" value="Satisfied"> Satisfied</input></td>
</tr>
<tr>
    <td> </td>
    <td><input type="radio" name="reservation" value="Dissatisfied"> Dissatisfied</input></td>
</tr>
<tr>
    <td> </td>
    <td><input type="radio" name="reservation" value="N/A"> N/A</input></td>
</tr>       
票数 1
EN

Stack Overflow用户

发布于 2014-04-22 22:57:25

你的逻辑有错误。你用了两次isset。

代码语言:javascript
复制
<input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if ((isset($_POST['hosting'])) && ((isset($_POST['hosting'])) == 1)) {echo 'checked="checked"';}; ?> /> 

您本质上是说isset等于1,在您的情况下,如果$_POST‘POST’有一个值,那么这个值总是正确的。

你应该这么做

代码语言:javascript
复制
<input type="radio" id="hosting-1" name="hosting[]" value="1" class="w100" <?php if (isset($_POST['hosting']) && $_POST['hosting'] == 1) {echo 'checked="checked"';} ?> /> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23231168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档