Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 确定上次选中的单选按钮_Javascript_Html - Fatal编程技术网

Javascript 确定上次选中的单选按钮

Javascript 确定上次选中的单选按钮,javascript,html,Javascript,Html,假设一组有4个无线电设备,第一个已选中。然后用户单击第三个收音机框。有没有办法知道第一个收音机是最后一个选中的收音机?如果您绑定到mousedown事件而不是单击,那么当事件侦听器运行时,选中的收音机将不会更改: <fieldset id="radios"> <input type="radio" name="rad" value="1"> option 1<br> <input type="radio" name="rad" value

假设一组有4个无线电设备,第一个已选中。然后用户单击第三个收音机框。有没有办法知道第一个收音机是最后一个选中的收音机?

如果您绑定到mousedown事件而不是单击,那么当事件侦听器运行时,选中的收音机将不会更改:

<fieldset id="radios">
    <input type="radio" name="rad" value="1"> option 1<br>
    <input type="radio" name="rad" value="2"> option 2<br>
    <input type="radio" name="rad" value="3"> option 3<br>
</fieldset>
注意:此解决方案是IE8+,请参见

如果您绑定到mousedown事件而不是单击,则当事件侦听器运行时,选中的收音机尚未更改:

<fieldset id="radios">
    <input type="radio" name="rad" value="1"> option 1<br>
    <input type="radio" name="rad" value="2"> option 2<br>
    <input type="radio" name="rad" value="3"> option 3<br>
</fieldset>

注意:此解决方案是IE8+,请参见声明全局变量

var currentlySelected = null;
2将处理程序附加到所有单选按钮的单击事件

function radioButtonOnClickEvent() {
    if (currentlySelected === null) { 
        // nothing has been selected yet
        currentlySelected = radioBoxThatWasJustClicked;
    } else if (currentlySelected === theThirdRadioButton) {
         //your logic here
         currentlySelected = radioBoxThatWasJustClicked;
    }
}

1声明一个全局变量

var currentlySelected = null;
2将处理程序附加到所有单选按钮的单击事件

function radioButtonOnClickEvent() {
    if (currentlySelected === null) { 
        // nothing has been selected yet
        currentlySelected = radioBoxThatWasJustClicked;
    } else if (currentlySelected === theThirdRadioButton) {
         //your logic here
         currentlySelected = radioBoxThatWasJustClicked;
    }
}

是的,有办法做到这一点。我已经添加了您的代码和完整的脚本,以获得最后一个选择单选按钮

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Test webservices</title>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 

    <script>
        $(document).ready(function () {

            $('input[name="rad"]').click(function (e) {
                if($(this).siblings('input[name="rad"]').hasClass('lastSelected'))
                {
                    var lastSelectedButton = $('.lastSelected').val();
                    $(this).siblings('input[name="rad"]').removeClass('lastSelected');
                    $(this).addClass('lastSelected');
                }
                else
                    $(this).addClass('lastSelected');
            });
        });

    </script>
</head>
<body>
    <form action='xyz.php' method='get'>
        <input type="radio" name="rad" value="1"> option 1<br>
        <input type="radio" name="rad" value="2"> option 2<br>
        <input type="radio" name="rad" value="3"> option 3<br>
        <input type='submit' value='Go' id='submit' >
    </form>

</body>
</html>

这是你问题的完美答案。您可以尝试通过复制粘贴此脚本来实现它。

是的,有一种方法可以做到这一点。我已经添加了您的代码和完整的脚本,以获得最后一个选择单选按钮

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Test webservices</title>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 

    <script>
        $(document).ready(function () {

            $('input[name="rad"]').click(function (e) {
                if($(this).siblings('input[name="rad"]').hasClass('lastSelected'))
                {
                    var lastSelectedButton = $('.lastSelected').val();
                    $(this).siblings('input[name="rad"]').removeClass('lastSelected');
                    $(this).addClass('lastSelected');
                }
                else
                    $(this).addClass('lastSelected');
            });
        });

    </script>
</head>
<body>
    <form action='xyz.php' method='get'>
        <input type="radio" name="rad" value="1"> option 1<br>
        <input type="radio" name="rad" value="2"> option 2<br>
        <input type="radio" name="rad" value="3"> option 3<br>
        <input type='submit' value='Go' id='submit' >
    </form>

</body>
</html>

这是你问题的完美答案。你可以试着用这个脚本的复制粘贴来实现它。

你在使用jQuery吗?@AndréAlçadaPadez:真的很离题,但要修正你的语法*是,*你需要以隐藏的方式记住它,并在你的事件中使用它。你在使用jQuery吗?@AndréAlçadaPadez:严重脱离主题,但要修正你的语法*是,*你需要记住它,可能是隐藏的,并在你的事件中使用它。它看起来比你的简短代码更重要。因为我已经给出了完整的html页面代码,所以jquery仍然是小而高效的。它看起来比你的短代码更简单。因为我已经给出了完整的html页面代码,所以jquery仍然是小而高效的。