在表单提交后保持单选按钮被选中 [英] keeping radio buttons checked after form submit

查看:158
本文介绍了在表单提交后保持单选按钮被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组单选按钮,分别是按钮按钮1 。我在下面的代码中使用

1.首先保存默认值 question1 answer2 用于下一个设置)

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



 < div id =button_set1> 
< input onClick =show_seq_lunid(); ('set'($ _ POST ['button'])&&& $ _POST ['button'] =='Yes')echo'检查= 选中';?>已检查/>< label> question1< / label>
< input onClick =show_list_lunid(); (= set($ _ POST ['button'])&&&&& $ _POST ['button'] =='No')echo'检查= 选中';?> /><标签>&ANSWER1 LT; /标签>
< / div>

< div id =button_set2>
< input onClick =os_hpux(); ('set'($ _ POST ['button1'])&& $ _POST ['button1'] =='Yes')echo'检查= 选中';?> /><标签>&问题2 LT; /标签>
< input onClick =os_others(); ('set'($ _POST ['button1'])&&& $ _POST ['button1'] =='No')echo'检查= 选中';?>已检查/>< label> answer2< / label>
< / div>

这里如果我使用下面的代码,第二个单选按钮 button1 在表单提交后不会粘在用户选择上,它会变回缺省状态。 answer2 。但第一组单选按钮工作正常。
如果我从代码中删除了默认的选中的选项,这两个单选按钮在表单提交后工作正常。如何在表单提交后检查单选按钮,同时使用选中无线电默认选项

解决方案

所以,问题在于你在表单提交时将选中的值设置为两次,导致选择默认值(从初始表单状态)或已提交的值。

为了正常工作,您需要始终使用PHP来附加已检查的

 < div id =button_set1> 
< input onClick =show_seq_lunid();如果(!isset($ _POST ['button'])||(isset($ _POST ['button'])&& $'='='radio'name =buttonvalue =Yes _POST ['button'] =='Yes'))echo'checked =checked'?> /><标签>&问题1 LT; /标签>
< input onClick =show_list_lunid(); (= set($ _ POST ['button'])&&&&& $ _POST ['button'] =='No')echo'检查= 选中';?> /><标签>&ANSWER1 LT; /标签>
< / div>

< div id =button_set2>
< input onClick =os_hpux(); ('set'($ _ POST ['button1'])&& $ _POST ['button1'] =='Yes')echo'检查= 选中';?> /><标签>&问题2 LT; /标签>
< input onClick =os_others();如果(!isset($ _POST ['button1'])||(isset($ _POST ['button1'])&& $($)'='='radio'name =button1value =No _POST ['button1'] =='No'))echo'checked =checked';?> /><标签>&ANSWER2 LT; /标签>
< / div>

以下是预览版: http://codepad.viper-7.com/rbInpX



另请注意,您正在使用嵌入式JavaScript表示法,通常不鼓励将动态JS内容分离并更易于管理; - )

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

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

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>

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

解决方案

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.

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>

Here's a working preview: http://codepad.viper-7.com/rbInpX

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

这篇关于在表单提交后保持单选按钮被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆