表单提交后保持选中单选按钮表单、单选、按钮、提交后

2023-09-06 19:12:09 作者:孤独,是一种态度

我有两组单选按钮,格式为 buttonbutton1.我正在使用下面的代码来

I have two set of radio buttons in html form button and button1. I am using below code to

1.保持默认值选中(question1为第一组,answer2为下一组)

1.keep the default value checked (question1 for first set and answer2 for next set)

2.表单提交后保持用户单选按钮选择

2.keep user radio button selection after the form submit

<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 在表单提交后不会粘在用户选择上,它会变回其默认选中状态.即 answer2.但是第一组单选按钮工作正常.如果我从代码中删除默认的 checked 选项,则表单提交后两个单选按钮都可以正常工作.在使用 checked 无线电的默认选项时,如何在表单提交后保持单选按钮处于选中状态

Here if i use below code, the second radio button button1 is not sticking on to the user selection after form submit, it changing back to its default checked state.ie answer2. But the first set of radio buttons work fine. If I remove the default checked option from the code, both radio buttons working fine after form submit. How can I keep the radio button checked after form submit while using checked default option for radios

推荐答案

所以,问题是您在表单提交时设置了两次 checked 值,导致选择默认值(从初始表单状态)或已提交的值.

So, the problem is you're setting the checked value twice upon form submission, resulting in selecting either the default value (from initial form state) or the value that has been submitted.

为了使其正常工作,您需要始终使用 PHP 将 checked 值附加到您的单选元素,如下所示:

For this to work correctly, you'd need always use PHP to append the checked value to your radio elements, like this:

<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 内容分离和更易于管理;-)

Also, please note that you're using inline JavaScript notation which is normally discouraged to keep dynamic JS content separate and more manageable ;-)