2013-09-30 69 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

我喜欢这种方法。先生,干得好。 –