如何显示选中的单选按钮仍然勾选?

时间:2011-08-26 00:39:10

标签: php html ajax

我的应用程序有2个单选按钮,当用户勾选其中任何一个时,勾选的单选按钮的值通过ajax保存在表中,现在的问题是,当用户返回页面时,单选按钮不是勾选,如何使选中的单选按钮显示为勾选,以便用户知道他之前选择了哪个?

这是我的代码

<ul>
    <li style="list-style-type: none;">    
<div align="center" class="radio_group">
    <input type="radio" id="gallerymenustyle1" class="element radio" name="gallerymenustyle[]" value="1" /> Gallery Link - In the navigation of my website, display one "gallery" link<br />
    <input type="radio" id="gallerymenustyle2" class="element radio" name="gallerymenustyle[]" value="2" /> Category Links - In the navigation of my website, display a separate link to each category.
</div>
    </li>
</ul>

2 个答案:

答案 0 :(得分:0)

当您在PHP中呈现用户的页面时,您需要查询保存选择的位置(可能是数据库)。如果用户选择了该特定单选按钮,则会将其显示为已选中。代码类似于:

// Do a database query or something to get the value that the user has stored before (if any)
<input type="radio" id="gallerymenustyle1" class="element radio" name="gallerymenustyle[]" value="1" <?php if ($gallerymenustyleFromDatabaseValue == 1){ echo 'selected'; }/> Gallery Link....<br />
<input type="radio" id="gallerymenustyle1" class="element radio" name="gallerymenustyle[]" value="2" <?php if ($gallerymenustyleFromDatabaseValue == 2){ echo 'selected'; }/> Category Link....

答案 1 :(得分:0)

如果用户已登录,则应查询数据库以获取该按钮是否已勾选的值。

如果用户未登录,您可以使用会话ID。虽然这只会在用户关闭浏览器/会话到期之前有效。

<?php
$checked = 0;
// this should come from db
$checked = 1;
?>

<ul>
  <li style="list-style-type: none;">    
    <div align="center" class="radio_group">
      <input type="radio" id="gallerymenustyle1" class="element radio" name="gallerymenustyle[]" value="1" <?php if ($checked == 1) print('checked="checked"') ?>/> Gallery Link - In the navigation of my website, display one "gallery" link<br />
      <input type="radio" id="gallerymenustyle2" class="element radio" name="gallerymenustyle[]" value="2" <?php if ($checked == 1) print('checked="checked"') ?>/> Category Links - In the navigation of my website, display a separate link to each category.
    </div>
  </li>
</ul>

将检查第一个单选按钮