javascript - Get previously checked Radio Button - Stack Overflow

Given a set of radio buttons, like:<input id="B_1" type="radio" name="radio

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 plete attempt: /

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 plete attempt: http://jsfiddle/H6h4R/

Share asked Jul 21, 2012 at 10:19 Rik PoggiRik Poggi 29.3k7 gold badges68 silver badges84 bronze badges 1
  • You have to rethink here. Everytime you click on a new radio - the checkValue just fills with the one you clicks. – Andreas Nilsson Commented Jul 21, 2012 at 10:30
Add a ment  | 

2 Answers 2

Reset to default 6

Try this - http://jsfiddle/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.

if he uses keyboard key this can help with that

http://jsfiddle/DYrW3/73/

$('input[name=radios]').keydown(function(){
        alert("Before change "+$('input[name=radios]:checked').val());
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744815742a4595313.html

相关推荐

  • javascript - Get previously checked Radio Button - Stack Overflow

    Given a set of radio buttons, like:<input id="B_1" type="radio" name="radio

    2天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信