获取先前检查的单选按钮 [英] Get previously checked Radio Button

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

问题描述

给出一组单选按钮,例如:

<input id="B_1" type="radio" name="radioName" value="1">
<input id="B_2" type="radio" name="radioName" value="2">
<input id="B_3" type="radio" name="radioName" value="3">
<input id="B_4" type="radio" name="radioName" value="4">

我尝试将以下函数附加到onclick事件,但是它不起作用,因为它返回了当前选中的单选按钮,而不是之前选择的那个单选按钮:

my_func = function() {
    var checkedValue = $('input[type=radio]:checked').val();
    return alert("The previously selected was: " + checkedValue);
  };

我不知道是否有帮助,无论如何,这是完整的尝试: http://jsfiddle.net /H6h4R/

解决方案

尝试一下- http://jsfiddle. net/H6h4R/1/

$(":radio").on("mousedown", function() {
    $("p").text( $('input[type=radio]:checked').val() );
}).on("mouseup", function() {
    $('input[type=radio]').prop('checked', false);
    $(this).prop('checked', true);
});

click事件仅在以下一系列确切的事件之后触发:

  • 当指针位于元素内部时,按下鼠标按钮.
  • 当指针位于元素内部时,释放鼠标按钮.

Given a set of radio buttons, like:

<input id="B_1" type="radio" name="radioName" value="1">
<input id="B_2" type="radio" name="radioName" value="2">
<input id="B_3" type="radio" name="radioName" value="3">
<input id="B_4" type="radio" name="radioName" value="4">

​When one of them gets checked is it possible to know which was previously active (if there is one)?

I tried to attach the following function to the onclick event, but it doesn't work because it returns the the presently selected radio button instead of the one that was selected before:

my_func = function() {
    var checkedValue = $('input[type=radio]:checked').val();
    return alert("The previously selected was: " + checkedValue);
  };

I don't know if could be of any help, anyway here's the complete attempt: http://jsfiddle.net/H6h4R/

解决方案

Try this - http://jsfiddle.net/H6h4R/1/

$(":radio").on("mousedown", function() {
    $("p").text( $('input[type=radio]:checked').val() );
}).on("mouseup", function() {
    $('input[type=radio]').prop('checked', false);
    $(this).prop('checked', true);
});

The click event is only triggered after this exact series of events:

  • The mouse button is depressed while the pointer is inside the element.
  • The mouse button is released while the pointer is inside the element.

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

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