【问题标题】:Radio buttons need to stay checked [closed]单选按钮需要保持选中[关闭]
【发布时间】:2015-08-15 00:07:24
【问题描述】:

我需要制作两个在提交后保持选中状态的单选按钮。我在一篇 Joomla 文章中做这个。我安装了 DirectPHP,所以我可以使用 php 和 javascript。我到现在为止的代码:

<form>
<input name="button" type="radio" value="1" id="button1" /> 
<input name="button" type="radio" value="2" id="button2" />
<input type="submit" value="submit" />
</form>

【问题讨论】:

  • 为什么要打开 2 个表单标签?
  • 请解释一下保持检查?转到下一页时必须检查它们吗?发布到服务器时必须检查它们吗?你想完成什么?
  • 提交后你必须用php检查是否在按钮1,$_POST['button1']==1如果是,你echo 'checked';在标签里面。
  • 旁注:如果省略该方法,表单默认为 GET
  • @Fred-ii- GET 评论?我有:or GET + 在他的问题的第一个版本中,他使用了POST

标签: php html radio-button joomla3.0


【解决方案1】:

仅供参考:
如果省略该方法,则表单默认为 GET。

  • &lt;form&gt;相当于做&lt;form method="get"&gt;

如果您的 PHP 使用 POST 数组,那么您需要指定方法并且没有在您的问题中提供任何其他代码。

即:&lt;form method="post"&gt;

像这样检查输入标签的内部:

<form>
<input name="button" type="radio" value="1" id="button1" <?php if($_GET["button"]==1 ){ echo "checked"; }?> /> 
<input name="button" type="radio" value="2" id="button2" <?php if($_GET["button"]==2 ){ echo "checked"; }?> />
<input type="submit" value="submit" />
</form>

根据您的需要更改名称和方法。

  • 旁注:如果您的 PHP 使用 POST 数组并指定“post”方法,请将 GET 更改为 POST

【讨论】:

  • 大声笑,完全一样的答案 :-) 你的答案更完整
  • @Fred-ii- 你是什么意思?比如小错字什么的?
  • 我明白了。我正在纠正它,谢谢
  • @Loko 表单默认为 GET。我现在在回答中使用 $_REQUEST
  • @Fred-ii- 由于随机的 JS 部分,我也花了很长时间才回答。我在编辑后 6 分钟回答。
【解决方案2】:
<form>
<input name="button" type="radio" value="1" id="button1"<?php if ($_REQUEST['button1'] === 1) echo " checked"; ?>/> 
<input name="button" type="radio" value="2" id="button2"<?php if ($_REQUEST['button2'] === 1) echo " checked"; ?>/>
<input type="submit" value="submit" />
</form>

【讨论】:

    【解决方案3】:

    <form>
      <input name="button1" type="radio" value="1" id="button1" <?php if ($_REQUEST[ 'button1']===1 ) echo "checked"; ?>/>
      <input name="button2" type="radio" value="2" id="button2" <?php if ($_REQUEST[ 'button2']===2 ) echo "checked"; ?>/>
      <input type="submit" value="submit" />
    </form>

    【讨论】:

      猜你喜欢
      • 2012-09-19
      • 2019-09-29
      • 1970-01-01
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      相关资源
      最近更新 更多