【问题标题】:keep radio button values and set default checked保持单选按钮值并设置默认选中
【发布时间】:2014-06-07 12:16:05
【问题描述】:

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

请注意,我使用<span class="custom-radiobutton"></span> 来设置单选按钮的样式,z-index 低于真正的<input type="radio",后者具有opacity:0

这是我的代码的 sn-p:

<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 验证可能更好)。
  • 我注意到我需要单击两次才能选择第一个单选按钮。这只发生在原始状态。在此之后,它可以一键运行,正如预期的那样。

我花费了大量时间试图找出问题所在,因此我们将非常感谢您提供任何帮助。 干杯,

【问题讨论】:

  • 你不需要为单选按钮做checked="checked",你可以使用checked。前任。

标签: php forms validation radio-button


【解决方案1】:
<input type="radio" id="hosting-1" name="hosting[]" class="w100"  checked="checked" /> 1

在你的代码中试试这个。

例如:

<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>       

【讨论】:

    【解决方案2】:

    您的逻辑有误。您使用 isset 两次。

    <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['hosting'] 具有值,则在您的情况下始终为真。

    你应该这样做

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

    【讨论】:

    • 我在您注意到我时编辑了我的逻辑(谢谢!),但是当我加载表单时,单选按钮仍然没有任何选项标记为已选中。这是我的头疼。
    • 你确定 $_POST['hosting'] 有值吗?
    • 我不确定@Robbert,因为我编码:echo "hosting: " . (boolean)$hosting ."&lt;br&gt;"; echo "domain: " . (boolean)$domain ."&lt;br&gt;"; ...而且我在提交时没有看到任何值。也许不是正确的测试方法?
    • 试试 echo "&lt;pre&gt;" . print_r($_POST,true) . "&lt;/pre&gt;"; 这将显示 $_POST 中的所有内容。
    • 我有解决方案,伙计们!这是代码:在第一个单选按钮中(默认未选中):&lt;?php if (isset($_POST['hosting']) &amp;&amp; $_POST['hosting'][0] == 1) {echo 'checked';} ?&gt; ...对于第二个单选按钮,(默认选中):&lt;?php if ( (isset($_POST['hosting']) &amp;&amp; $_POST['hosting'][0] == 0) || (!isset($_POST['hosting'])) ) {echo 'checked';} ?&gt;
    猜你喜欢
    • 1970-01-01
    • 2021-05-30
    • 2020-08-10
    • 2014-02-25
    • 1970-01-01
    • 2023-03-05
    • 2021-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多