Html 表单提交后保持单选按钮处于选中状态

Html 表单提交后保持单选按钮处于选中状态,html,forms,radio-button,Html,Forms,Radio Button,我有两套html格式的单选按钮button和button1。我正在使用下面的代码 1.保持默认值处于选中状态(question1用于第一组,而answer2用于下一组) 2.表单提交后保留用户单选按钮选择 <div id="button_set1"> <input onClick="show_seq_lunid();" type="radio" name="button" value="Yes" <?php if(isset($_POST['button']) &

我有两套html格式的单选按钮
button
button1
。我正在使用下面的代码

1.保持默认值处于选中状态
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>

/>答复1
选中/>回答2
在这里,如果我使用下面的代码,那么表单提交后,第二个单选按钮
button1
不会粘在用户选择上,它会变回默认的选中状态。即
answer2
。但第一组单选按钮工作正常。
如果我从代码中删除默认的
选中的
选项,两个单选按钮在表单提交后都可以正常工作。当使用收音机的
checked
default选项时,如何在表单提交后保持单选按钮处于选中状态?因此,问题是您在表单提交时两次设置
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>

/>答复1
/>答复2
以下是一个工作预览:


另外,请注意,您正在使用内联JavaScript表示法,通常不鼓励使用内联JavaScript表示法来保持动态JS内容独立且更易于管理;-)

我最近遇到了一个类似的问题,但是有更多的单选按钮需要处理,所以我想我应该将值检查功能抽象为一个方法,该方法也可以创建单选按钮本身,从而最小化重复的值检查代码。我已经修改了我的,以适合您的变量名:

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>
函数createRadioOption($name、$value、$onClickMethodName、$labelText){
$checked='';
如果((isset($\u POST[$name])&&&$\u POST[$name]==$value)){
$checked='checked=“checked”';
}
回声(“.$labelText.”);
}

你需要一些服务器端的魔法。您是否在后端使用php?您必须通过表单处理返回元素状态。您的逻辑是正确的,但应用不正确。。。如果没有人能在10分钟内回答,我会。。。吃atm:D