单选按钮返回到先前的选择 [英] Radio buttons return to previous selection

查看:236
本文介绍了单选按钮返回到先前的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我希望这些单选按钮的工作方式如下:当选择一个(在window.confirm之后)并且确认被取消时,返回到之前选定的单选按钮。



编辑:

我有一个3个单选按钮,当您选择其中任何一个时,会出现一个确认框。当确认被取消时,我希望选中的单选按钮成为新选择之前的单选按钮。 试试这个:

javascript:

  var radioVal =none; 
var radioConfirm ='';

函数示例(e)
{
if(e.checked == true)
{
if(confirm(这是您的值想要选择?))
{
radioVal = e.value;
radioConfirm = e;
}
else
{
radioConfirm.checked = true;
}
}
}

HTML:

  1< input type =radioonclick =javascript:example(this)value ='1 'name ='test'/> 
2< input type =radioonclick =javascript:example(this)value ='2'name ='test'/>
3< input type =radioonclick =javascript:example(this)value ='3'name ='test'/>

当确认框被确认后,以前的DOM元素将被存储(假设返回)。每当您取消确认框时,您之前存储的DOM元素将被选中,并且选中将被设置为true。



如果您第一次取消,则没有先前的选择,所以你甚至可以检查:

  if(!radioConfirm =='')
{
radioConfirm.checked = true;
}
else
{
e.checked = false;

$ / code>

如果您之前没有确认过这张支票, p>

jsFiddle






正如您在@OlivierH的答案中看到jQuery,它使用起来更容易。所以这不是浪费:)

so I want these radio buttons to work like this: When one is selected (after window.confirm) and the confirm is cancelled, to return to the previously selected radio button.

EDIT:

I have a 3 radio buttons, that when you select any of them a confirm box comes up. When the confirm is cancelled, I would like the checked radio button to be the one previous to the new selection.

解决方案

Try this:

javascript:

var radioVal = "none";
var radioConfirm = '';

function example(e)
{
       if(e.checked == true)
       {
          if(confirm("Is this the value you want to choose?"))
          {
              radioVal = e.value;
              radioConfirm = e;
          }
           else
           {
             radioConfirm.checked = true; 
           }
       }
}

Html:

1 <input type="radio" onclick="javascript:example(this)" value='1' name='test'/>
2 <input type="radio" onclick="javascript:example(this)" value='2' name='test'/>
3 <input type="radio" onclick="javascript:example(this)" value='3' name='test'/>

The previous DOM element will be stored when the confirm box has been confirmed(given true back). Whenever you cancel the confirm box, your previous stored DOM element will be selected and checked will be set to true.

If you cancel for the first time there is no previous selection, so you can even check on that:

if(!radioConfirm == '')
{
  radioConfirm.checked = true; 
}
else
{
  e.checked = false; 
}

This way the check will be unchecked if you haven't confirmed before

jsFiddle


As you see jQuery with answer of @OlivierH it's much easier to use. So it's not a waste to get into that :)

这篇关于单选按钮返回到先前的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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