2011-08-26 73 views
0

我的應用程序有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> 
+0

你說:'當用戶回來到page'但如果你使用AJAX的用戶不應該遠離它。或者你的意思是在稍後的階段? – PeeHaa

+0

假設你是我的應用程序的用戶,那麼你勾選了任何單選按鈕..它被保存了..然後你離開瀏覽應用程序的不同頁面,現在當你回到那個頁面時,單選按鈕,你勾選的單選按鈕沒有被選中,所以如何解決這個問題? – sasori

回答

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

將檢查第一個單選按鈕