2013-09-30 68 views
1

我有兩組html03形式的單選按鈕buttonbutton1。我使用下面的代碼來表單提交後保持單選按鈕被檢查

1.keep默認值檢查question1爲第一組和answer2爲下集)的形式後

2.保持用戶選擇單選按鈕提交

<div id="button_set1"> 
<input onClick="show_seq_lunid();" type="radio" name="button" value="Yes" <?php if(isset($_POST['button']) && $_POST['button'] == 'Yes') echo ' checked="checked"';?> checked /><label>question1</label> 
<input onClick="show_list_lunid();" type="radio" name="button" value="No" <?php if(isset($_POST['button']) && $_POST['button'] == 'No') echo ' checked="checked"';?> /><label>answer1</label> 
</div> 

<div id="button_set2"> 
<input onClick="os_hpux();" type="radio" name="button1" value="Yes" <?php if(isset($_POST['button1']) && $_POST['button1'] == 'Yes') echo ' checked="checked"';?> /><label>question2</label> 
<input onClick="os_others();" type="radio" name="button1" value="No" <?php if(isset($_POST['button1']) && $_POST['button1'] == 'No') echo ' checked="checked"';?> checked /><label>answer2</label> 
</div> 

這裏,如果我使用下面的代碼,第二個單選按鈕button1不會粘在表單提交後的用戶選擇上,它會變回默認的選中狀態.ie answer2。但第一組單選按鈕工作正常。 如果我從代碼中刪除默認的checked選項,那麼這兩個單選按鈕在表單提交後工作正常。我怎樣才能保持表格後檢查的單選按鈕,而提交使用無線電

+0

您需要一些服務器端魔術。你在後端使用PHP嗎? – codefreak

+0

您必須將表單處理返回給您的元素狀態。 –

+0

是的我使用php – acr

回答

2

所以,問題是你在表單提交設置checked值的兩倍,從而選擇將默認值checked默認選項(從最初的形式狀態)或已提交的值。

爲了使其正常工作,你需要一直使用PHP的checked值添加到您的無線電元素,像這樣:

<div id="button_set1"> 
<input onClick="show_seq_lunid();" type="radio" name="button" value="Yes" <?php if(!isset($_POST['button']) || (isset($_POST['button']) && $_POST['button'] == 'Yes')) echo ' checked="checked"'?> /><label>question1</label> 
<input onClick="show_list_lunid();" type="radio" name="button" value="No" <?php if(isset($_POST['button']) && $_POST['button'] == 'No') echo ' checked="checked"';?> /><label>answer1</label> 
</div> 

<div id="button_set2"> 
<input onClick="os_hpux();" type="radio" name="button1" value="Yes" <?php if(isset($_POST['button1']) && $_POST['button1'] == 'Yes') echo ' checked="checked"';?> /><label>question2</label> 
<input onClick="os_others();" type="radio" name="button1" value="No" <?php if(!isset($_POST['button1']) || (isset($_POST['button1']) && $_POST['button1'] == 'No')) echo ' checked="checked"';?> /><label>answer2</label> 
</div> 

這裏有一個工作預覽:http://codepad.viper-7.com/rbInpX

另外,請請注意,您正在使用內嵌JavaScript表示法,通常不鼓勵將動態JS內容分開並更易於管理;-)

+0

謝謝!它的工作.. – acr

+0

高興地幫助:) –

1

我有一個類似的專業版但最近有更多的單選按鈕需要處理,所以我認爲我會將值檢查功能抽象爲一種方法,該方法還會創建單選按鈕本身,從而最大限度地減少重複的值檢查代碼。我已經重新編寫了我的以適合您的變量名稱:

function createRadioOption($name, $value, $onClickMethodName, $labelText) { 

    $checked = ''; 
    if ((isset($_POST[$name]) && $_POST[$name] == $value)) { 
    $checked = ' checked="checked"'; 
    } 

    echo('<input onClick="'. $onClickMethodName .'();" type="radio" name="'. $name .'" value="'. $value .'"'. $checked .' /><label>'. $labelText .'</label>'); 
} 

<div id="button_set1"> 
<?php createRadioOption('button', 'Yes', 'show_seq_lunid', 'question1'); ?> 
<?php createRadioOption('button', 'No', 'show_list_lunid', 'question1'); ?> 
</div> 

<div id="button_set2"> 
<?php createRadioOption('button1', 'Yes', 'os_hpux', 'question2'); ?> 
<?php createRadioOption('button1', 'No', 'os_others', 'question2'); ?> 
</div> 
+0

我喜歡這種方法。先生,幹得好。 –