简体   繁体   中英

Check previously clicked radio button

I have a form having multiple radio buttons in different groups. eg category vehicle has two radio buttons

  1. 2 wheeler
  2. 4 wheeler

same as other categories have 2-2 radio buttons.

What i want to do is when i check 4 wheeler from 2 wheeler i show a warning message pop up, are u sure to switch, if yes then ok, but for no i want to check again 2 wheeler radio button. I can not do this from id as well coz i have to do this on other fields as well.

<input type="radio"/>

Your questions is not very clear but from what I understand I guess you want:

$(document).ready(function(){
$('#submit_button').click(function() {
    if (!$("input[@name='radioName']:checked").val()) {
     alert('Nothing is checked!');
    return false;
    }
else {
  alert('One of the radio buttons is checked!');
}
 });
 });

And add the same name tag for all your radio inputs:

<input type="radio" name="radioName"/>

This might be what you're trying to achieve:

var old_input = $('input[name="wheeler"]:checked');
$('input[name="wheeler"]').change(function(e) {
    console.log(e);
    if (
        old_input.next().text() == '2 Wheeler' &&
        $(this).next().text() == '4 Wheeler'
    ) {
        if (confirm('Are you sure you want to change?')) {
            old_input = $(this);
        }
        else {
            this.checked = 0;
            old_input.get()[0].checked = 1;
        }
    }
    else {
        old_input = $(this);
    }
});

http://jsfiddle.net/pQpk7/

I think you are looking for this http://jsfiddle.net/7rF3d/ When a radio button is checked for the first time nothing happens. When someone checked another he is first prompted for a confirmation. If the user cancels the old checked radio will be restored.

var lastChecked;

$("input[type=radio]").click(function() {

    if(typeof(lastChecked) != "undefined" && !confirm("are you sure?")) {

        $("input[type=radio]:checked").attr("checked", "false");

        $(lastChecked).prop("checked", true);

    } else {

        lastChecked = $('input[type=radio]:checked');

    }

});

Since jQuery isn't tagged in the question, here's a pure JS approach. Guys, seriously, try to quit suggesting jQuery solutions when it isn't even tagged in the question.

You can achieve this by using the click event and returning false if there is at least one radio button checked, then the answer to a window.confirm is false, which will prevent it to select the new one as expected.

var oneChecked=false;
var elem=document.getElementsByName("wheeler");

for (var i=0;i<elem.length;i++) {
   elem[i].onclick=function(e) {
       if (oneChecked && !window.confirm("Are you sure to switch?")) {
           return false;
       } else {
           oneChecked=true;
       }
   }
}

FIDDLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM